How to use current_package_name method in ATX

Best Python code snippet using ATX

helpers.py

Source:helpers.py Github

copy

Full Screen

1"""2repackage.helpers3-----------------4"""5import os6import rdflib7import shutil8import simplejson9import tempfile10import urllib211import zipfile12import commonware.log13from django.conf import settings14from django.http import Http40415from django.template.defaultfilters import slugify16from xpi import xpi_utils17log = commonware.log.getLogger('f.repackage')18def increment_version(version):19 """20 Modify version string to indicate changes made by repacking21 :attr: version (string) version to be modified22 """23 version = str(version)24 version_split = version.split('.')25 if len(version_split) == 2:26 return '%s.1' % version27 if len(version_split) == 1:28 return '%s.0.1' % version29 try:30 int(version_split[-1])31 except ValueError:32 return '%s.1' % version33 else:34 version_split[-1] = str(int(version_split[-1]) + 1)35 return '.'.join(version_split)36class Extractor(object):37 """38 Extracts manifest from ``install.rdf``39 modified ``Extractor`` class from ``zamboni/apps/versions/compare.py``40 """41 manifest = u'urn:mozilla:install-manifest'42 ADDON_EXTENSION = '2'43 def __init__(self, install_rdf):44 self.rdf = rdflib.Graph().parse(install_rdf)45 self.find_root()46 # TODO: check if it's a JetPack addon47 self.data = {}48 def read_manifest(self, package_overrides={}):49 """50 Extracts data from ``install.rdf``, assignes it to ``self.data``51 :param: target_version (String) forces the version52 :returns: dict53 """54 data = {55 # since SDK 1.0b5 id has no longer be synced with public key56 'id': self.find('id'),57 'type': self.find('type') or self.ADDON_EXTENSION,58 'fullName': self.find('name'),59 # read version and increment the version number60 'version': increment_version(self.find('version')),61 'url': self.find('homepageURL'),62 'description': self.find('description'),63 'author': self.find('creator'),64 'license': self.find('license'),65 'lib': self.find('lib') or settings.JETPACK_LIB_DIR,66 'data': self.find('data') or settings.JETPACK_DATA_DIR,67 'tests': self.find('tests') or 'tests',68 #'packages': self.find('packages') or 'packages',69 'main': self.find('main') or 'main',70 'no_restart': True,71 }72 for key, value in data.items():73 if value or package_overrides.get(key, None):74 self.data[key] = package_overrides.get(key, None) or value75 return self.data76 def uri(self, name):77 namespace = 'http://www.mozilla.org/2004/em-rdf'78 return rdflib.term.URIRef('%s#%s' % (namespace, name))79 def find_root(self):80 # If the install-manifest root is well-defined, it'll show up when we81 # search for triples with it. If not, we have to find the context that82 # defines the manifest and use that as our root.83 # http://www.w3.org/TR/rdf-concepts/#section-triples84 manifest = rdflib.term.URIRef(self.manifest)85 if list(self.rdf.triples((manifest, None, None))):86 self.root = manifest87 else:88 self.root = self.rdf.subjects(None, self.manifest).next()89 def find(self, name, ctx=None):90 """Like $() for install.rdf, where name is the selector."""91 if ctx is None:92 ctx = self.root93 # predicate it maps to <em:{name}>.94 match = list(self.rdf.objects(ctx, predicate=self.uri(name)))95 # These come back as rdflib.Literal, which subclasses unicode.96 if match:97 return unicode(match[0])98class Repackage(object):99 def download(self, location):100 """101 Downloads the XPI (from ``location``) and102 instantiates XPI in ``self.xpi_zip``103 This eventually will record statistics about build times104 :param: location (String) location of the file to download rebuild105 ``XPI``106 :returns: None107 """108 log.info("Downloading file to rebuild from (%s)" % location)109 try:110 xpi_remote_file = urllib2.urlopen(location,111 timeout=settings.URLOPEN_TIMEOUT)112 except IOError, err:113 log.warning("Downloading XPI (%s) for rebuild failed\n(%s)" %114 (location, str(err)))115 raise116 else:117 # this check is needed as urlopen behaves different for118 # URLs starting with file:// (we use it in tests)119 if (hasattr(xpi_remote_file, 'getcode')120 and xpi_remote_file.getcode()):121 if xpi_remote_file.getcode() == 404:122 log.warning("URL does not exist (%s)" % location)123 raise Http404124 elif xpi_remote_file.getcode() != 200:125 log.warning("URL (%s) could not be open (%s)" %126 (location, xpi_remote_file.getcode()))127 raise Http404128 # zipfile doesn't work on the urllib filelike entities129 self.xpi_temp = tempfile.TemporaryFile()130 self.xpi_temp.write(xpi_remote_file.read())131 self.xpi_zip = zipfile.ZipFile(self.xpi_temp)132 xpi_remote_file.close()133 def retrieve(self, xpi_from):134 """135 Handles upload136 :param: xpi_from (element of request.FILES)137 """138 self.xpi_temp = tempfile.TemporaryFile()139 for chunk in xpi_from.chunks():140 self.xpi_temp.write(chunk)141 self.xpi_zip = zipfile.ZipFile(self.xpi_temp)142 def rebuild(self, sdk_source_dir, hashtag, package_overrides={},143 options=None):144 """145 Drive the rebuild process146 :param: sdk_source_dir (String) absolute path of the SDK147 :param: hashtag (String) filename for the buid XPI148 :param: target_version (String)149 """150 self.get_manifest(package_overrides=package_overrides)151 sdk_dir = self.extract_packages(sdk_source_dir)152 log.debug('[%s] Manifest: %s' % (hashtag, self.manifest))153 # build xpi154 log.debug("[%s] Rebuilding XPI" % hashtag)155 response = xpi_utils.build(sdk_dir,156 os.path.join(sdk_dir, 'packages', self.manifest['name']),157 self.manifest['name'], hashtag, options=options)158 # here find the created XPI and compare main dir list159 # if not the same - copy the files from original XPI160 target = os.path.join(settings.XPI_TARGETDIR, '%s.xpi' % hashtag)161 target_zip = zipfile.ZipFile(target, 'a')162 target_namelist = target_zip.namelist()163 for f in self.xpi_zip.namelist():164 if len(f.split('/')) == 1 and f not in target_namelist:165 log.debug(f)166 # export file from original xpi and pack file into target xpi167 with tempfile.NamedTemporaryFile() as extr_file:168 extr_file.write(self.xpi_zip.open(f).read())169 target_zip.write(extr_file.name, f)170 target_zip.close()171 # clean up (sdk_dir is already removed)172 log.debug("[%s] Done rebuilding XPI; cleaning up" % hashtag)173 self.cleanup()174 return response175 def get_manifest(self, package_overrides={}):176 """177 extracts manifest from ``install.rdf`` it does not contain all178 dependencies, these will be appended during copying package files179 Sets the ``self.manifest`` field180 """181 # extract data provided in install.rdf182 install_rdf = self.xpi_zip.open('install.rdf')183 extracted = Extractor(install_rdf)184 self.manifest = extracted.read_manifest(185 package_overrides=package_overrides)186 # get data provided by harness-options.json187 ho_json = self.xpi_zip.open('harness-options.json')188 self.harness_options = simplejson.loads(ho_json.read())189 ho_json.close()190 # ``name`` is provided since SDK 1.0b2, it needs to be generated from191 # ``fullName`` for older add-ons192 self.manifest['name'] = self.harness_options.get('name',193 slugify(self.manifest['fullName']))194 # add default dependency195 self.manifest['dependencies'] = ['addon-kit', 'api-utils']196 def extract_packages(self, sdk_source_dir):197 """Builds SDK environment and calls the :meth:`xpi.xpi_utils.build`198 :returns: temporary sdk_dir199 """200 def get_package_dir(dir_name, current_package_name):201 "returns the target path to the lib, data etc. dirs"202 return os.path.join(sdk_dir, 'packages', current_package_name,203 self.manifest[dir_name])204 # create temporary directory for SDK205 sdk_dir = tempfile.mkdtemp()206 # copy contents of the SDK dir207 for d in os.listdir(sdk_source_dir):208 s_d = os.path.join(sdk_source_dir, d)209 if os.path.isdir(s_d):210 shutil.copytree(s_d, os.path.join(sdk_dir, d))211 else:212 shutil.copy(s_d, sdk_dir)213 # do not copy these SDK packages from XPI214 sdk_dependencies = ['addon-kit', 'api-utils']215 # these main dir files do not need to be copied from XPI216 standard_main_dir_files = [217 'bootstrap.js', 'harness-options.json', 'install.rdf']218 package_name = self.manifest['name']219 # find a prefix used to create directories in the XPI220 uri_prefix = "resources/%s-" % (221 self.manifest['id'].split('@')[0].lower())222 # SDK 1.0 changed the resource naming convention223 uri_prefix_1 = "resources/%s-" % (224 self.manifest['id'].lower().replace('@', '-at-'))225 # if uri prefix is not standarized it's name is placed in226 # harness-options file227 uri_prefix_1 = (self.harness_options.get('uriPrefix', uri_prefix_1)228 .replace('resource://', 'resources/', 1))229 # help lists to collect dependencies230 exporting = []231 dependencies = []232 # collect main dir files here233 main_dir_files = []234 def _extract(f, name, resource_dir_prefix):235 """ extract file236 :attr: f (string) full path of the file within XPI237 :attr: name (string) 'data', 'docs', 'lib' or 'tests'238 :attr: resource_dir_prefix (string)239 """240 if name not in f or resource_dir_prefix not in f:241 # copy files from main directory242 if (f.count('/') == 0 and f not in standard_main_dir_files):243 f_dir = os.path.join(244 sdk_dir, 'packages', package_name)245 if not os.path.isdir(f_dir):246 os.makedirs(f_dir)247 f_name = os.path.join(f_dir, f)248 if not os.path.exists(f_name):249 with open(f_name, 'w') as f_file:250 f_file.write(self.xpi_zip.open(f).read())251 main_dir_files.append(f)252 return253 # get current package name from directory name (f)254 current_package_name = '-'.join(f.split(255 resource_dir_prefix)[1].split('/')[0].split('-')[:-1])256 # do not extract SDK libs257 if current_package_name in sdk_dependencies:258 return259 # create lib, data and tests directories260 if (current_package_name, name) not in exporting:261 # create appropriate directory262 somedir = get_package_dir(name, current_package_name)263 if not os.path.isdir(somedir):264 os.makedirs(somedir)265 exporting.append((current_package_name, name))266 # create minimal package.json for dependencies267 # it contains only name of the package268 if (current_package_name != package_name269 and current_package_name not in dependencies):270 # collect info about exported dependencies271 dependencies.append(current_package_name)272 # create package.json273 try:274 p_meta = self.harness_options['metadata'].get(275 current_package_name, None)276 except Exception, err:277 log.error("No metadata about dependency "278 "(%s) required by (%s)\n%s" % (279 current_package_name, package_name, str(err)))280 return281 with open(os.path.join(sdk_dir, 'packages',282 current_package_name, 'package.json'), 'w') as manifest:283 manifest.write(simplejson.dumps(p_meta))284 # create aprropriate subdirectories and export files285 resource_dir = lambda x: "%s%s-%s/" % (286 resource_dir_prefix, current_package_name, x)287 # proceed with files from within right directory288 if f.startswith(resource_dir(name)) and f != resource_dir(name):289 # create target file name290 f_name = "%s/%s" % (291 get_package_dir(name, current_package_name),292 f.split(resource_dir(name))[1])293 # if f is a directory, create it only - needed for empty dirs294 if f.endswith('/'):295 if not os.path.isdir(f_name):296 os.makedirs(f_name)297 return298 # if directory containing the file does not exist - create it299 parent_dir = '/'.join(f_name.split('/')[:-1])300 if not os.path.isdir(parent_dir):301 os.makedirs(parent_dir)302 # export file303 with open(f_name, 'w') as f_file:304 f_file.write(self.xpi_zip.open(f).read())305 # run _extract for every file and directory in the XPI306 for f in self.xpi_zip.namelist():307 # compatible with SDK <= 1.0b1308 resource_dir_prefix = uri_prefix309 # compatible with SDK > 1.0b1310 if uri_prefix != uri_prefix_1 and uri_prefix_1 in f:311 resource_dir_prefix = uri_prefix_1312 for name in ['docs', 'lib', 'data', 'tests']:313 _extract(f, name, resource_dir_prefix)314 # Add all dependencies to the manifest315 # This is a flat list - hierarchy might be different from original316 self.manifest['dependencies'].extend(dependencies)317 # Add icon files to manifest318 for f in main_dir_files:319 if 'icon' in f:320 self.manifest[f.split('.')[0]] = f321 # create add-on's package.json file322 log.debug('Writing manifest %s, %s' % (os.path.join(323 sdk_dir, 'packages', package_name, 'package.json'),324 self.manifest))325 package_dir = os.path.join(326 sdk_dir, 'packages', package_name)327 try:328 with open(os.path.join(package_dir, 'package.json'), 'w') as manifest:329 manifest.write(simplejson.dumps(self.manifest))330 except:331 log.critical("Manifest couldn't be exported to %s" % package_path)332 raise333 return sdk_dir334 def cleanup(self):335 """closes all files opened during the repackaging336 """337 self.xpi_zip.close()...

Full Screen

Full Screen

main.py

Source:main.py Github

copy

Full Screen

1import re2import os3import networkx as nx4import matplotlib.pyplot as plt5import argparse6parser = argparse.ArgumentParser(description="Generate an import graph of the internal import of some Go code")7parser.add_argument('scanDir', metavar='inputDirectory', type=str, help='directory to scan')8parser.add_argument('-o', dest='outputFile', default=None, help='name of the output file')9args = parser.parse_args()10OUTPUT_FILE = args.outputFile11SCAN_DIR = args.scanDir12print("Discovering files...")13files = []14for r, d, f in os.walk(SCAN_DIR):15 for fname in f:16 if '.go' in fname:17 files.append(os.path.join(r, fname))18import_regex = r"import ?\((.*?)\)"19module_regex = r"module ?(.*)"20print("Searching for go.mod...")21try:22 with open(os.path.join(SCAN_DIR, "go.mod"), encoding="utf8") as f:23 r = re.findall(module_regex, f.read(), re.MULTILINE)24 if len(r) != 0:25 BASE_PACKAGE = r[0]26except FileNotFoundError:27 pass28finally:29 if BASE_PACKAGE == "":30 BASE_PACKAGE = input("Input module name: ").strip()31package_regex = re.compile(BASE_PACKAGE.replace(".", "\.") + r"(.*)")32all_packages = []33imports_dict = {}34for fname in files:35 f = open(fname, encoding="utf8")36 fname = fname.replace(SCAN_DIR, "")37 print(f"Scanning file {fname}... ")38 current_package_name = fname.replace(os.path.sep, "/")39 current_package_name = "/".join(current_package_name.split("/")[:-1])40 matches = re.findall(import_regex, f.read(), re.DOTALL)41 if len(matches) == 0:42 # No imports section found43 pass44 else:45 # Extract import block46 import_statements = matches[0].strip().split("\n")47 for index, statement in enumerate(import_statements):48 # Make a list of import statements without quotes49 import_statements[index] = statement.strip().replace("\"", "")50 # internal_import_statements will be a list of packages that the current file imports51 internal_import_statements = []52 # Filter by imports that are from the current project53 for statement in import_statements:54 v = package_regex.findall(statement)55 if len(v) != 0:56 internal_import_statements.append(v[0])57 if current_package_name in imports_dict:58 for s in internal_import_statements:59 imports_dict[current_package_name] = imports_dict[current_package_name] + internal_import_statements60 else:61 imports_dict[current_package_name] = internal_import_statements62 if current_package_name not in all_packages:63 all_packages.append(current_package_name)64 f.close()65print("Generating graph...")66# Generate graph67edge_list = []68for key in imports_dict:69 for ims in imports_dict[key]:70 edge_list.append((key.strip("/"), ims.strip("/")))71red_nodes = []72other_nodes = []73for key in all_packages:74 if "cmd" in key and key not in red_nodes:75 red_nodes.append(key)76 elif key not in other_nodes:77 other_nodes.append(key)78graph = nx.DiGraph() # Directional graph79graph.add_edges_from(edge_list)80pos = nx.kamada_kawai_layout(graph)81nx.draw_networkx_nodes(graph, pos, nodelist=other_nodes)82nx.draw_networkx_nodes(graph, pos, nodelist=red_nodes, node_color="r")83nx.draw_networkx_labels(graph, pos)84nx.draw_networkx_edges(graph, pos, edgelist=graph.edges(), arrows=True, arrowstyle="-|>")85if OUTPUT_FILE is None:86 plt.show()87else:88 print(f"Saving to {OUTPUT_FILE}...")...

Full Screen

Full Screen

parse_debian_source_packages.py

Source:parse_debian_source_packages.py Github

copy

Full Screen

1"""2Parses the native debian sources format into panbuild's project format.3"""4import json5# import os6import fileinput7if __name__ == '__main__':8 source_packages = {}9 current_package_name = ""10 current_package = {}11 for line in fileinput.input():12 line_parts = line.split(':')13 if len(line_parts) < 2:14 if current_package_name:15 source_packages[current_package_name] = current_package16 current_package_name = ""17 current_package = {}18 continue19 field_name = line_parts[0]20 field_value = ':'.join(line_parts[1:])21 field_value = field_value.strip(' \n')22 if current_package_name:23 if field_name == 'Package':24 # Finish the current package and start a new one.25 source_packages[current_package_name] = current_package26 current_package_name = ""27 current_package = {}28 continue29 if field_name == 'Vcs-Git':30 current_package['vcs_urls'].append(field_value)31 if field_name == 'Vcs-Browser' or field_name == 'Homepage':32 current_package['web_urls'].append(field_value)33 if field_name == 'Maintainer':34 current_package['maintainers'].append(field_value)35 if field_name == 'Binary':36 for artifact_name in field_value.split(','):37 current_package['artifact_names'].append(artifact_name.strip())38 if field_name == 'Version':39 current_package['versions'].append(field_value)40 else:41 if field_name == 'Package':42 current_package_name = field_value43 current_package['name'] = current_package_name44 current_package['id'] = current_package_name45 current_package['description'] = ""46 current_package['summary'] = ""47 current_package['keywords'] = []48 current_package['versions'] = []49 current_package['artifact_names'] = []50 current_package['maintainers'] = []51 current_package['web_urls'] = []52 current_package['vcs_urls'] = []53 current_package['is_core'] = False54 current_package['layer'] = 455 continue...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run ATX automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful