forked from coala/coala-bears
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_package.py
166 lines (138 loc) · 6.54 KB
/
generate_package.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import argparse
import os
import shutil
from pathlib import Path
from string import Template
import subprocess
import sys
import time
from bears import VERSION
from dependency_management.requirements.PipRequirement import PipRequirement
from coalib.collecting.Importers import iimport_objects
from coalib.parsing.Globbing import glob
def create_file_from_template(template_file, output_file, substitution_dict):
"""
Creates a file from a template file, using a substitution dict.
:param template_file: The template file.
:param output_file: The file to be written.
:param substitution_dict: The dict from which the substitutions are taken.
"""
with open(template_file) as fl:
template = fl.read()
template = Template(template).safe_substitute(**substitution_dict)
with open(output_file, 'w') as output_handle:
output_handle.write(template)
def create_file_structure_for_packages(root_folder, file_to_copy, object_name):
"""
Creates a file structure for the packages to be uploaded. The structure
will be ``root_folder/object_name/coalaobject_name/object_name.py``.
Also holds a ``root_folder/object_name/coalaobject_name/__init__.py``
to make the package importable.
:param root_folder: The folder in which the packages are going to be
generated.
:param file_to_copy: The file that is going to be generated the package
for.
:param object_name: The name of the object that is inside the
file_to_copy.
"""
upload_package_folder = os.path.join(
root_folder, object_name, 'coala' + object_name)
os.makedirs(upload_package_folder, exist_ok=True)
Path(os.path.join(upload_package_folder, '__init__.py')).touch()
shutil.copyfile(file_to_copy, os.path.join(upload_package_folder,
object_name + '.py'))
def perform_register(path, file_name):
"""
Register the directory to PyPi, after creating a ``sdist`` and
a ``bdist_wheel``.
:param path: The file on which the register should be done.
"""
subprocess.call(
[sys.executable, 'setup.py', 'sdist', 'bdist_wheel'], cwd=path)
subprocess.call(['twine', 'register', '-r', 'pypi', os.path.join(
path, 'dist', file_name + '.tar.gz')])
subprocess.call(['twine', 'register', '-r', 'pypi', os.path.join(
path, 'dist', file_name + '-py3-none-any.whl')])
def perform_upload(path):
"""
Uploads the directory to PyPi.
:param path: The folder in which the upload should be done.
"""
subprocess.call(
['twine', 'upload', path + '/dist/*'])
def create_upload_parser():
"""
Creates a parser for command line arguments.
:return: Parser arguments.
"""
parser = argparse.ArgumentParser(
description='Generates PyPi packages from bears.')
parser.add_argument('-r', '--register',
help='Register the packages on PyPi',
action='store_true')
parser.add_argument('-u', '--upload', help='Upload the packages on PyPi',
action='store_true')
parser.add_argument('bears',
help='Path to bears directory')
parser.add_argument('template', metavar='setup.py.in',
help='Path to setup.py.in template file')
return parser
def main():
args = create_upload_parser().parse_args()
os.chdir(os.path.join(args.bears, '..'))
os.makedirs(os.path.join('bears', 'upload'), exist_ok=True)
bear_version = VERSION
if 'dev' in bear_version:
bear_version = bear_version[:bear_version.find('dev')] + (
str(int(time.time())))
else:
bear_version = repr(bear_version) + '.' + str(int(time.time()))
for bear_file_name in sorted(set(glob('bears/**/*Bear.py'))):
bear_object = next(iimport_objects(
bear_file_name, attributes='kind', local=True),
None)
if bear_object:
bear_name, _ = os.path.splitext(os.path.basename(bear_file_name))
create_file_structure_for_packages(
os.path.join('bears', 'upload'), bear_file_name, bear_name)
if bear_object.REQUIREMENTS:
for requirement in bear_object.REQUIREMENTS:
if isinstance(requirement, PipRequirement):
with open(os.path.join(
'bears', 'upload',
bear_name, 'requirements.txt'),
'a') as reqtxt:
reqtxt.write(
requirement.package + '=='
+ requirement.version + '\n')
if os.path.exists(os.path.join('bears', 'upload',
bear_name, 'requirements.txt')):
with open(os.path.join(
'bears', 'upload',
bear_name, 'MANIFEST.in'), 'w') as manifest:
manifest.write('include requirements.txt')
substitution_dict = {'NAME': repr(bear_name),
'VERSION': bear_version,
'AUTHORS': str(bear_object.AUTHORS),
'AUTHORS_EMAILS':
str(bear_object.AUTHORS_EMAILS),
'MAINTAINERS': str(bear_object.maintainers),
'MAINTAINERS_EMAILS':
str(bear_object.maintainers_emails),
'PLATFORMS': str(bear_object.PLATFORMS),
'LICENSE': str(bear_object.LICENSE),
'LONG_DESCRIPTION': str(bear_object.__doc__),
'BEAR_NAME': bear_name,
'ENTRY': 'coala' + bear_name}
create_file_from_template(args.template,
os.path.join('bears', 'upload',
bear_name, 'setup.py'),
substitution_dict)
bear_dist_name = bear_name + '-' + bear_version
if args.register:
perform_register(os.path.join('bears', 'upload', bear_name),
bear_dist_name)
if args.upload:
perform_upload(os.path.join('bears', 'upload', bear_name))
if __name__ == '__main__': # pragma: no cover
sys.exit(main())