ยปCore Development>Code coverage>Lib/packaging/command/install_distinfo.py

Python code coverage for Lib/packaging/command/install_distinfo.py

#countcontent
1n/a"""Create the PEP 376-compliant .dist-info directory."""
2n/a
3n/a# Forked from the former install_egg_info command by Josip Djolonga
4n/a
5n/aimport os
6n/aimport csv
7n/aimport hashlib
8n/afrom shutil import rmtree
9n/a
10n/afrom packaging import logger
11n/afrom packaging.command.cmd import Command
12n/a
13n/a
14n/aclass install_distinfo(Command):
15n/a
16n/a description = 'create a .dist-info directory for the distribution'
17n/a
18n/a user_options = [
19n/a ('install-dir=', None,
20n/a "directory where the the .dist-info directory will be created"),
21n/a ('installer=', None,
22n/a "the name of the installer"),
23n/a ('requested', None,
24n/a "generate a REQUESTED file"),
25n/a ('no-requested', None,
26n/a "do not generate a REQUESTED file"),
27n/a ('no-record', None,
28n/a "do not generate a RECORD file"),
29n/a ('no-resources', None,
30n/a "do not generate a RESOURCES file"),
31n/a ]
32n/a
33n/a boolean_options = ['requested', 'no-record', 'no-resources']
34n/a
35n/a negative_opt = {'no-requested': 'requested'}
36n/a
37n/a def initialize_options(self):
38n/a self.install_dir = None
39n/a self.installer = None
40n/a self.requested = None
41n/a self.no_record = None
42n/a self.no_resources = None
43n/a self.outfiles = []
44n/a
45n/a def finalize_options(self):
46n/a self.set_undefined_options('install_dist',
47n/a 'installer', 'requested', 'no_record')
48n/a
49n/a self.set_undefined_options('install_lib', 'install_dir')
50n/a
51n/a if self.installer is None:
52n/a # FIXME distutils or packaging?
53n/a # + document default in the option help text above and in install
54n/a self.installer = 'distutils'
55n/a if self.requested is None:
56n/a self.requested = True
57n/a if self.no_record is None:
58n/a self.no_record = False
59n/a if self.no_resources is None:
60n/a self.no_resources = False
61n/a
62n/a metadata = self.distribution.metadata
63n/a
64n/a basename = metadata.get_fullname(filesafe=True) + ".dist-info"
65n/a
66n/a self.install_dir = os.path.join(self.install_dir, basename)
67n/a
68n/a def run(self):
69n/a target = self.install_dir
70n/a
71n/a if os.path.isdir(target) and not os.path.islink(target):
72n/a if not self.dry_run:
73n/a rmtree(target)
74n/a elif os.path.exists(target):
75n/a self.execute(os.unlink, (self.install_dir,),
76n/a "removing " + target)
77n/a
78n/a self.execute(os.makedirs, (target,), "creating " + target)
79n/a
80n/a metadata_path = os.path.join(self.install_dir, 'METADATA')
81n/a self.execute(self.distribution.metadata.write, (metadata_path,),
82n/a "creating " + metadata_path)
83n/a self.outfiles.append(metadata_path)
84n/a
85n/a installer_path = os.path.join(self.install_dir, 'INSTALLER')
86n/a logger.info('creating %s', installer_path)
87n/a if not self.dry_run:
88n/a with open(installer_path, 'w') as f:
89n/a f.write(self.installer)
90n/a self.outfiles.append(installer_path)
91n/a
92n/a if self.requested:
93n/a requested_path = os.path.join(self.install_dir, 'REQUESTED')
94n/a logger.info('creating %s', requested_path)
95n/a if not self.dry_run:
96n/a open(requested_path, 'wb').close()
97n/a self.outfiles.append(requested_path)
98n/a
99n/a if not self.no_resources:
100n/a install_data = self.get_finalized_command('install_data')
101n/a if install_data.get_resources_out() != []:
102n/a resources_path = os.path.join(self.install_dir,
103n/a 'RESOURCES')
104n/a logger.info('creating %s', resources_path)
105n/a if not self.dry_run:
106n/a with open(resources_path, 'w') as f:
107n/a writer = csv.writer(f, delimiter=',',
108n/a lineterminator='\n',
109n/a quotechar='"')
110n/a for row in install_data.get_resources_out():
111n/a writer.writerow(row)
112n/a
113n/a self.outfiles.append(resources_path)
114n/a
115n/a if not self.no_record:
116n/a record_path = os.path.join(self.install_dir, 'RECORD')
117n/a logger.info('creating %s', record_path)
118n/a if not self.dry_run:
119n/a with open(record_path, 'w', encoding='utf-8') as f:
120n/a writer = csv.writer(f, delimiter=',',
121n/a lineterminator='\n',
122n/a quotechar='"')
123n/a
124n/a install = self.get_finalized_command('install_dist')
125n/a
126n/a for fpath in install.get_outputs():
127n/a if fpath.endswith('.pyc') or fpath.endswith('.pyo'):
128n/a # do not put size and md5 hash, as in PEP-376
129n/a writer.writerow((fpath, '', ''))
130n/a else:
131n/a size = os.path.getsize(fpath)
132n/a with open(fpath, 'rb') as fp:
133n/a hash = hashlib.md5()
134n/a hash.update(fp.read())
135n/a md5sum = hash.hexdigest()
136n/a writer.writerow((fpath, md5sum, size))
137n/a
138n/a # add the RECORD file itself
139n/a writer.writerow((record_path, '', ''))
140n/a self.outfiles.append(record_path)
141n/a
142n/a def get_outputs(self):
143n/a return self.outfiles