示例#1
0
    def test(self):
        # Parse spec file
        self.sp_obj = SpecParser(self.spec)
        if not self.sp_obj.parse():
            self.err = self.sp_obj.getError()
            return False

        # Parse sources file
        if self.sources != "":
            self.src_obj = Sources(self.sources)
            if not self.src_obj.parse():
                self.err = self.src_obj.getError()
                return False

        # Inspect tarball
        self.prj_obj = ProjectInfo(noGodeps=self.noGodeps)
        # create a temp directory
        dir = tempfile.mkdtemp()
        # extract tarball to the directory
        so, se, rc = runCommand("tar -xf %s --directory=%s" %
                                (self.archive, dir))
        if rc != 0:
            self.err = se
            return False

        so, se, rc = runCommand("ls %s" % dir)
        if rc != 0:
            self.err = "Unable to archive's extracted folder"
            return False

        so = so.split('\n')[0]
        if not self.prj_obj.retrieve("%s/%s" % (dir, so), skip_errors=True):
            self.err = self.prj_obj.getError()
            return False

        shutil.rmtree(dir)

        tests = []

        # test package name
        tests.append(self.testPackageName)
        # test commit
        tests.append(self.testCommit)
        # test import path macros
        tests.append(self.testImportPathMacros)
        # test provider, provider_tld, ...
        tests.append(self.testRepositoryMacros)
        # test source0 macro
        tests.append(self.testSpecFileSource)
        # test devel subpackage
        tests.append(self.testDevel)

        for test in tests:
            if not test():
                self.printTResult(self.t_result)
            elif self.verbose:
                self.printTResult(self.t_result)

        return True
示例#2
0
文件: GoLint.py 项目: jedahan/gofed
	def test(self):
		# Parse spec file
		self.sp_obj = SpecParser(self.spec)
		if not self.sp_obj.parse():
			self.err = self.sp_obj.getError()
			return False

		# Parse sources file
		if self.sources != "":
			self.src_obj = Sources(self.sources)
			if not self.src_obj.parse():
				self.err = self.src_obj.getError()
				return False

		# Inspect tarball
		self.prj_obj = ProjectInfo(noGodeps = self.noGodeps)
		# create a temp directory
		dir = tempfile.mkdtemp()
		# extract tarball to the directory
		so, se, rc = runCommand("tar -xf %s --directory=%s" % (self.archive, dir))
		if rc != 0:
			self.err = se
			return False

		so, se, rc = runCommand("ls %s" % dir)
		if rc != 0:
			self.err = "Unable to archive's extracted folder"
			return False

		so = so.split('\n')[0]
		if not self.prj_obj.retrieve("%s/%s" % (dir, so), skip_errors = True):
			self.err = self.prj_obj.getError()
			return False

		shutil.rmtree(dir)

		tests = []

		# test package name
		tests.append(self.testPackageName)
		# test commit
		tests.append(self.testCommit)
		# test import path macros
		tests.append(self.testImportPathMacros)
		# test provider, provider_tld, ...
		tests.append(self.testRepositoryMacros)
		# test source0 macro
		tests.append(self.testSpecFileSource)
		# test devel subpackage
		tests.append(self.testDevel)

		for test in tests:
			if not test():
				self.printTResult(self.t_result)
			elif self.verbose:
				self.printTResult(self.t_result)

		return True
示例#3
0
    def decodeProject(self, working_directory="."):
        if not self.repository_decoded:
            self.err = "RepositoryInfo not decoded"
            return False

        # get package info
        self.project_info = ProjectInfo(self.noGodeps)
        source_code_directory = "%s/%s" % (working_directory, self.archive_dir)
        if not os.path.exists(source_code_directory):
            self.err = "Source code directory %s does not exist." % source_code_directory
            self.err += "CWD: %s" % os.getcwd()
            return False

        if not self.project_info.retrieve(source_code_directory):
            self.err = self.project_info.getError()
            return False

        return True
  def loadMetaData( self ):
    '''
    Load metadata from the drictory .scientificProjectManager and return initialized objects of ProjectInfo, ProjectLog, ProjectGraph and DataFileOperation 
    '''
    #Create objects of the metadat handlers
    self.pI = ProjectInfo( '.scientificProjectManager/projectInfo.xml' )
    self.pL = ProjectLog( '.scientificProjectManager/projectLog.xml' )
    self.pG = ProjectGraph( '.scientificProjectManager/projectGraph.xml' )
    self.dO = DataFileOperation( self.pI, self.pL )
    
    #Register destruct functions 
    atexit.register( self.pI.destruct )
    atexit.register( self.pL.destruct )
    atexit.register( self.pG.destruct )

   
    return  ( self.pI, self.pL, self.pG, self.dO )
示例#5
0
	def decodeProject(self, working_directory = "."):
		if not self.repository_decoded:
			self.err = "RepositoryInfo not decoded"
			return False

		# get package info
		self.project_info = ProjectInfo(self.noGodeps)
		source_code_directory = "%s/%s" % (working_directory, self.archive_dir)
		if not os.path.exists(source_code_directory):
			self.err = "Source code directory %s does not exist." % source_code_directory
			self.err += "CWD: %s" % os.getcwd()
			return False

		if not self.project_info.retrieve(source_code_directory, self.skip_errors):
			self.err = self.project_info.getError()
			return False

	
		return True
class MetaDataManager():
  '''
  MetaDataManager manages the metadata of a scientific project
  '''
  def __init__( self ):
    return
  
  def init( self, a_currentHost ):
    '''
    Create meta data for a research project
    '''
    #If the metadata directory is already been created, return
    if os.path.exists( '.scientificProjectManager' ):
      print "The directory .scientificProjectManager is already been created!"
      return 
    
    #Create data and metadata directories 
    subprocess.call( [ 'mkdir', 'data' ] )
    subprocess.call( [ 'mkdir', 'data/externalData' ] )
    subprocess.call( [ 'mkdir', 'data/manuallyProcessedData' ] )
    subprocess.call( [ 'mkdir', '.scientificProjectManager' ] )
    subprocess.call( [ 'mkdir', '.scientificProjectManager/scratch' ] )
    subprocess.call( [ 'mkdir', '.scientificProjectManager/scripts' ] )
    subprocess.call( [ 'mkdir', '.scientificProjectManager/scripts/steps' ] )
    subprocess.call( [ 'mkdir', '.scientificProjectManager/scripts/showData' ] )
    subprocess.call( [ 'mkdir', '.scientificProjectManager/scripts/utilities' ] )
    
    #Create metadata files 
    self.createNewProjectInfo( a_currentHost )
    self.createNewProjectLog( a_currentHost )
    self.createNewProjectGraph()
    return

  def createNewProjectInfo( self, a_currentHost ):
    '''
    create a new projectInfo.xml file
    '''
    #If the projectInfo.xml file is already been created, return
    if os.path.exists( '.scientificProjectManager/projectInfo.xml' ):
      print "The .scientificProjectManager/projectInfo.xml is already been created!"
      return

    #Create the XML tree of the project information
    projectInfo      = ET.Element('projectInfo')
    hostInfo         = ET.SubElement( projectInfo, 'hostInfo' )
    currentHost      = ET.SubElement( hostInfo, 'currentHost' )
    hostList         = ET.SubElement( hostInfo, 'hostList'    )
    host             = ET.SubElement( hostList, a_currentHost )
    homeAbsPath      = ET.SubElement( host, 'homeAbsPath'     )
   
    currentHost.text = a_currentHost
    homeAbsPath.text = os.path.abspath('.')

    #Save the projectInfo.xml file
    fXml = open('.scientificProjectManager/projectInfo.xml', 'w')
    fXml.write( utilities.getPrettyXmlString( projectInfo ) )
    fXml.close() 
    return
  
  def createNewProjectLog( self, a_currentHost ):
    '''
    create a new projectLog.xml file
    '''
    #If the projectLog.xml file is already been created, return
    if os.path.exists( '.scientificProjectManager/projectLog.xml' ):
      print "The .scientificProjectManager/projectLog.xml is already been created!"
      return

    #Create the XML tree of the project log
    projectLog = ET.Element('projectLog')
    record     = ET.SubElement( projectLog, 'record' )
    timeE      = ET.SubElement( record, 'time'       )
    host       = ET.SubElement( record, 'host'       )
    event      = ET.SubElement( record, 'event'      )
    timeE.text = time.strftime('%Y_%m_%d_%H_%M_%S')
    host.text  = a_currentHost
    event.text = "Create metadata"

    #Save the projectLog.xml file
    fXml = open('.scientificProjectManager/projectLog.xml', 'w')
    fXml.write( utilities.getPrettyXmlString( projectLog ) )
    fXml.close() 
    return

  def createNewProjectGraph( self ):
    '''
    create a new projectGraph.xml file
    '''
    #If the projectGraph.xml file is already been created, return
    if os.path.exists( '.scientificProjectManager/projectGraph.xml' ):
      print "The .scientificProjectManager/projectGraph.xml is already been created!"
      return

    #Create the XML tree of the project graph
    projectGraph = ET.Element('projectGraph')
    nodes        = ET.SubElement( projectGraph, 'nodes' )
    edges        = ET.SubElement( projectGraph, 'edges' )

    #Save the projectGraph.xml file
    fXml = open('.scientificProjectManager/projectGraph.xml', 'w')
    fXml.write( utilities.getPrettyXmlString( projectGraph ) )
    fXml.close() 
    return

  def loadMetaData( self ):
    '''
    Load metadata from the drictory .scientificProjectManager and return initialized objects of ProjectInfo, ProjectLog, ProjectGraph and DataFileOperation 
    '''
    #Create objects of the metadat handlers
    self.pI = ProjectInfo( '.scientificProjectManager/projectInfo.xml' )
    self.pL = ProjectLog( '.scientificProjectManager/projectLog.xml' )
    self.pG = ProjectGraph( '.scientificProjectManager/projectGraph.xml' )
    self.dO = DataFileOperation( self.pI, self.pL )
    
    #Register destruct functions 
    atexit.register( self.pI.destruct )
    atexit.register( self.pL.destruct )
    atexit.register( self.pG.destruct )

   
    return  ( self.pI, self.pL, self.pG, self.dO )

  def syncMetadataToRemote( self, a_host ):
    '''
    Synchronize metadata to the remote host.
    Metadata must be load before calling this function 
    '''
    #Temporarily change the current host into the remote host
    currentHost = self.pI.getCurrentHost()
    self.pI.setCurrentHost( a_host )
    #Save metaData files
    self.pI.destruct()
    self.pL.destruct()
    self.pG.destruct()
    #Change the currenthost name back
    self.pI.setCurrentHost( currentHost )
    
    utilities.syncFilesToRemote( self.pI.getHomeAbsPath(), ['SMPInterface.py','.scientificProjectManager'], a_host, self.pI.getHomeAbsPath(a_host) )
    
    #Restore the correct project info file
    self.pI.destruct()
    return

  def createRemoteWorkSpace( self, a_host, a_homeAddr ):
    '''
    Create a remote work space on a given host and copy all metadata there.
    '''
    if self.pI.hostExists( a_host ):
      print 'The remote host '+a_host+' already exists!'
      return
    self.pI.addHost( a_host, a_homeAddr )
    currentHost    = self.pI.getCurrentHost()
    currentHomeAbs = self.pI.getHomeAbsPath() 

    #This is a trick to create a empty data directory in the remote host
    subprocess.call( [ 'rsync', '-av', os.path.join( currentHomeAbs, '.scientificProjectManager/scratch' ), a_host+':'+os.path.join(a_homeAddr,'data') ] )  
    
    #Temporarily change the current host into the remote host
    self.pI.setCurrentHost( a_host )
    self.pI.saveToFile( self.pI.xmlFileName )
    #Synchronize the metadata and the interface to the remote host
    utilities.syncFilesToRemote( currentHomeAbs, ['SMPInterface.py','.scientificProjectManager'], a_host, a_homeAddr)

    #Change the currenthost name back
    self.pI.setCurrentHost( currentHost )
     
    return
示例#7
0
class GoLint(Base):
    def __init__(self,
                 spec,
                 sources,
                 archive,
                 verbose=False,
                 stdout=True,
                 noGodeps=[]):
        Base.__init__(self)
        self.spec = spec
        self.sources = sources
        self.archive = archive
        self.verbose = verbose
        self.stdout = stdout
        self.test_results = []
        self.t_result = ""
        self.noGodeps = noGodeps

        self.err_number = 0
        self.warn_number = 0

        self.sp_obj = None
        self.src_obj = None
        self.prj_obj = None

    def getErrorCount(self):
        return self.err_number

    def getWarningCount(self):
        return self.warn_number

    def getTestResults(self):
        return self.test_results

    def printTResult(self, t_result):
        if type(t_result) == type([]):
            if self.stdout:
                print "\n".join(t_result)
            else:
                self.test_results += t_result
        else:
            if self.stdout:
                print t_result
            else:
                self.test_results.append(t_result)

    def test(self):
        # Parse spec file
        self.sp_obj = SpecParser(self.spec)
        if not self.sp_obj.parse():
            self.err = self.sp_obj.getError()
            return False

        # Parse sources file
        if self.sources != "":
            self.src_obj = Sources(self.sources)
            if not self.src_obj.parse():
                self.err = self.src_obj.getError()
                return False

        # Inspect tarball
        self.prj_obj = ProjectInfo(noGodeps=self.noGodeps)
        # create a temp directory
        dir = tempfile.mkdtemp()
        # extract tarball to the directory
        so, se, rc = runCommand("tar -xf %s --directory=%s" %
                                (self.archive, dir))
        if rc != 0:
            self.err = se
            return False

        so, se, rc = runCommand("ls %s" % dir)
        if rc != 0:
            self.err = "Unable to archive's extracted folder"
            return False

        so = so.split('\n')[0]
        if not self.prj_obj.retrieve("%s/%s" % (dir, so), skip_errors=True):
            self.err = self.prj_obj.getError()
            return False

        shutil.rmtree(dir)

        tests = []

        # test package name
        tests.append(self.testPackageName)
        # test commit
        tests.append(self.testCommit)
        # test import path macros
        tests.append(self.testImportPathMacros)
        # test provider, provider_tld, ...
        tests.append(self.testRepositoryMacros)
        # test source0 macro
        tests.append(self.testSpecFileSource)
        # test devel subpackage
        tests.append(self.testDevel)

        for test in tests:
            if not test():
                self.printTResult(self.t_result)
            elif self.verbose:
                self.printTResult(self.t_result)

        return True

    def testPackageName(self):
        name = self.sp_obj.getTag('name')
        url = self.sp_obj.getTag('url')

        if name == "":
            self.t_result = "E: Missing name tag"
            self.err_number += 1
            return False

        if url == "":
            self.t_result = "E: Missing url tag"
            self.err_number += 1
            return False

        ip_obj = ImportPath(url)
        if not ip_obj.parse():
            self.err = ip_obj.getError()
            return False

        pkg_name = ip_obj.getPackageName()
        if pkg_name == '':
            self.t_result = "E: Uknown repo url"
            self.err_number += 1
            return False

        if pkg_name != name:
            self.t_result = "W: Incorrect package name %s, should be %s" % (
                name, pkg_name)
            self.warn_number += 1
            return False

        self.t_result = "I: Package name correct"
        return True

    def testCommit(self):
        commit_label = 'commit'
        commit = self.sp_obj.getMacro(commit_label)
        if commit == "":
            commit_label = 'rev'
            commit = self.sp_obj.getMacro(commit_label)

        if commit == "":
            self.t_result = "E: missing %global commit or rev"
            self.err_number += 1
            return False

        if commit_label == "commit":
            if self.sp_obj.getMacro("shortcommit") == "":
                self.t_result = "E: missing %global shortcommit"
                self.err_number += 1
                return False
            self.t_result = "I: commit and shortcommit macro"
        else:
            if self.sp_obj.getMacro("shortrev") == "":
                self.t_result = "E: missing %global shortrev"
                self.err_number += 1
                return False
            self.t_result = "I: rev and shortrev macro"

        return True

    def testImportPathMacros(self):
        devel_prefix = self.sp_obj.getMacro('devel_prefix')
        if devel_prefix == "":
            import_path = self.sp_obj.getMacro('import_path')
            ip_macro = "import_path"
        else:
            import_path = self.sp_obj.getMacro('%s_import_path' % devel_prefix)
            ip_macro = "%s_import_path" % devel_prefix

        if import_path == "":
            self.t_result = "E: Missing %%{%s} macro" % ip_macro
            self.err_number += 1
            return False

        self.t_result = "I: %s macro found" % ip_macro
        return True

    def testRepositoryMacros(self):
        provider = self.sp_obj.getMacro('provider')
        if provider == "":
            self.t_result = "E: Missing %{provider} macro"
            self.err_number += 1
            return False

        provider_tld = self.sp_obj.getMacro('provider_tld')
        if provider_tld == "":
            self.t_result = "E: Missing %{provider_tld} macro"
            self.err_number += 1
            return False

        repo = self.sp_obj.getMacro('repo')
        if repo == "":
            self.t_result = "E: Missing %{repo} macro"
            self.err_number += 1
            return False

        project = self.sp_obj.getMacro('project')
        if project == "":
            self.t_result = "E: Missing %{project} macro"
            self.err_number += 1
            return False

        self.t_result = "I: provider, provider_tld, repo and project macros found"
        return True

    def testSpecFileSource(self):
        source = self.sp_obj.getTag('source')
        if source == "":
            self.t_result = "E: Missing source tag"
            self.err_number += 1
            return False

        archive = path.basename(source)

        if self.sources != "":
            sources = self.src_obj.getFiles()

            if archive not in sources:
                self.t_result = "E: archive in source[0] tag not in sources file"
                self.err_number += 1
                return False

        if archive != self.archive:
            self.t_result = "E: sources[0]'s tarball != specified tarball: %s != %s" % (
                archive, self.archive)
            self.err_number += 1
            return False

        self.t_result = "I: %s archive found in sources" % archive
        return True

    def testDevel(self):
        # get provided and imported paths from tarball
        t_imported = self.prj_obj.getImportedPackages()
        t_provided = self.prj_obj.getProvidedPackages()
        devel_prefix = self.sp_obj.getMacro('devel_prefix')
        if devel_prefix == "":
            import_path = self.sp_obj.getMacro('import_path')
        else:
            import_path = self.sp_obj.getMacro('%s_import_path' % devel_prefix)

        t_imported = filter(lambda i: not i.startswith(import_path),
                            t_imported)
        t_imported = map(lambda i: str("golang(%s)" % i), t_imported)

        skipped_provides_with_prefix = Config().getSkippedProvidesWithPrefix()

        for provide_prefix in skipped_provides_with_prefix:
            t_provided = filter(lambda i: not i.startswith(provide_prefix),
                                t_provided)

        t_provided = map(
            lambda i: str("golang(%s/%s)" % (import_path, i))
            if i != "." else str("golang(%s)" % import_path), t_provided)
        # get provides and [B]R from package section
        devel_obj = self.sp_obj.getDevelSubpackage()
        if devel_obj == None:
            self.t_result = "E: Unable to find devel subpackage"
            self.err_number += 1
            return False

        s_br = filter(lambda l: l != "golang", devel_obj.getBuildRequires())
        s_r = devel_obj.getRequires()
        s_provided = devel_obj.getProvides()

        # compare
        self.t_result = []
        # BR
        super_br = list(set(s_br) - set(t_imported) - set(['golang']))
        missing_br = list(set(t_imported) - set(s_br))
        for br in sorted(missing_br):
            self.t_result.append("W: Missing BuildRequires: %s" % br)
            self.warn_number += 1

        for br in sorted(super_br):
            self.t_result.append("W: Superfluous BuildRequires: %s" % br)
            self.warn_number += 1

        # R
        super_r = list(set(s_r) - set(t_imported) - set(['golang']))
        missing_r = list(set(t_imported) - set(s_r))
        for r in sorted(missing_r):
            self.t_result.append("W: Missing Requires: %s" % r)
            self.warn_number += 1

        for r in sorted(super_r):
            self.t_result.append("W: Superfluous Requires: %s" % r)
            self.warn_number += 1

        # Provides
        super_p = list(set(s_provided) - set(t_provided))
        missing_p = list(set(t_provided) - set(s_provided))
        for p in sorted(missing_p):
            self.t_result.append("W: Missing Provides: %s" % p)
            self.warn_number += 1

        for p in sorted(super_p):
            self.t_result.append("W: Superfluous Provides: %s" % p)
            self.warn_number += 1

        if self.t_result != []:
            return False

        self.t_result = "I: devel's provides checked"
        return True
示例#8
0
class PackageInfo:
    """
	Get basic information about project:
		imported packages
		provided packages
		tests
	"""
    def __init__(self, import_path, commit="", noGodeps=[]):
        self.import_path = import_path
        self.commit = commit
        self.noGodeps = noGodeps
        self.err = ""
        self.repository_info = None
        self.project_info = None
        self.archive_dir = ""
        self.repository_decoded = False
        self.name = ""

    def getError(self):
        return self.err

    def getName(self):
        return self.name

    def getRepositoryInfo(self):
        return self.repository_info

    def getProjectInfo(self):
        return self.project_info

    def decodeRepository(self):
        # get repository info
        self.repository_info = RepositoryInfo(self.import_path, self.commit)
        if not self.repository_info.retrieve():
            self.err = self.repository_info.getError()
            return False

        # package name
        ip_info = self.repository_info.getImportPathInfo()

        r_info = self.repository_info.getArchiveInfo()

        self.repository_decoded = True
        self.archive_dir = r_info.archive_dir
        self.name = ip_info.getPackageName()
        return True

    def decodeProject(self, working_directory="."):
        if not self.repository_decoded:
            self.err = "RepositoryInfo not decoded"
            return False

        # get package info
        self.project_info = ProjectInfo(self.noGodeps)
        source_code_directory = "%s/%s" % (working_directory, self.archive_dir)
        if not os.path.exists(source_code_directory):
            self.err = "Source code directory %s does not exist." % source_code_directory
            self.err += "CWD: %s" % os.getcwd()
            return False

        if not self.project_info.retrieve(source_code_directory):
            self.err = self.project_info.getError()
            return False

        return True
示例#9
0
                        fileText["LoanInfo"]["loan_period"],
                        fileText["LoanInfo"]["interest_compounded_number"])

    employee = Employee(
        fileText["ManagementInfo"]["employees"]["number"],
        fileText["ManagementInfo"]["employees"]["average_salary"])
    managementInfo = ManagementInfo(
        heatNetwork, fileText["ManagementInfo"]["energy_source_unit_price"],
        fileText["ManagementInfo"]["water_unit_price"], employee,
        fileText["ManagementInfo"]["water_replenish_rate"],
        fileText["ManagementInfo"]["device_depreciation_rate"],
        fileText["ManagementInfo"]["device_upkeep_rate"],
        fileText["ManagementInfo"]["other_expense_rate"],
        fileText["ManagementInfo"]["liquidity_rate"])

    taxInfo = TaxInfo(
        fileText["SalesInfo"]["TaxInfo"]["income_tax_rate"],
        fileText["SalesInfo"]["TaxInfo"]["value_added_tax_rate"],
        fileText["SalesInfo"]["TaxInfo"]
        ["urban_maintenance_construction_tax_rate"],
        fileText["SalesInfo"]["TaxInfo"]["education_supplementary_tax_rate"])

    projectInfo = ProjectInfo(
        fileText["ProjectInfo"]["project_life_cycle"],
        fileText["ProjectInfo"]["project_construction_cycle"])

    financialEvaluator = FinancialEvaluator(projectInfo, heatNetwork,
                                            managementInfo, salesInfo,
                                            loanInfo, taxInfo)
    financialEvaluator.Financial_Evaluate()
示例#10
0
文件: GoLint.py 项目: jedahan/gofed
class GoLint(Base):

	def __init__(self, spec, sources, archive, verbose = False, stdout = True, noGodeps = []):
		Base.__init__(self)
		self.spec = spec
		self.sources = sources
		self.archive = archive
		self.verbose = verbose
		self.stdout = stdout
		self.test_results = []
		self.t_result = ""
		self.noGodeps = noGodeps

		self.err_number = 0
		self.warn_number = 0

		self.sp_obj = None
		self.src_obj = None
		self.prj_obj = None

	def getErrorCount(self):
		return self.err_number

	def getWarningCount(self):
		return self.warn_number

	def getTestResults(self):
		return self.test_results

	def printTResult(self, t_result):
		if type(t_result) == type([]):
			if self.stdout:
				print "\n".join(t_result)
			else:
				self.test_results += t_result
		else:
			if self.stdout:
				print t_result
			else:
				self.test_results.append(t_result)

	def test(self):
		# Parse spec file
		self.sp_obj = SpecParser(self.spec)
		if not self.sp_obj.parse():
			self.err = self.sp_obj.getError()
			return False

		# Parse sources file
		if self.sources != "":
			self.src_obj = Sources(self.sources)
			if not self.src_obj.parse():
				self.err = self.src_obj.getError()
				return False

		# Inspect tarball
		self.prj_obj = ProjectInfo(noGodeps = self.noGodeps)
		# create a temp directory
		dir = tempfile.mkdtemp()
		# extract tarball to the directory
		so, se, rc = runCommand("tar -xf %s --directory=%s" % (self.archive, dir))
		if rc != 0:
			self.err = se
			return False

		so, se, rc = runCommand("ls %s" % dir)
		if rc != 0:
			self.err = "Unable to archive's extracted folder"
			return False

		so = so.split('\n')[0]
		if not self.prj_obj.retrieve("%s/%s" % (dir, so), skip_errors = True):
			self.err = self.prj_obj.getError()
			return False

		shutil.rmtree(dir)

		tests = []

		# test package name
		tests.append(self.testPackageName)
		# test commit
		tests.append(self.testCommit)
		# test import path macros
		tests.append(self.testImportPathMacros)
		# test provider, provider_tld, ...
		tests.append(self.testRepositoryMacros)
		# test source0 macro
		tests.append(self.testSpecFileSource)
		# test devel subpackage
		tests.append(self.testDevel)

		for test in tests:
			if not test():
				self.printTResult(self.t_result)
			elif self.verbose:
				self.printTResult(self.t_result)

		return True

	def testPackageName(self):
		name = self.sp_obj.getTag('name')
		url = self.sp_obj.getTag('url')

		if name == "":
			self.t_result = "E: Missing name tag"
			self.err_number += 1
			return False

		if url == "":
			self.t_result = "E: Missing url tag"
			self.err_number += 1
			return False

		ip_obj = ImportPath(url)
		if not ip_obj.parse():
			self.err = ip_obj.getError()
			return False

		pkg_name = ip_obj.getPackageName()
		if pkg_name == '':
			self.t_result = "E: Uknown repo url"
			self.err_number += 1
			return False

		if pkg_name != name:
			self.t_result = "W: Incorrect package name %s, should be %s" % (name, pkg_name)
			self.warn_number += 1
			return False

		self.t_result = "I: Package name correct"
		return True

	def testCommit(self):
		commit_label = 'commit'
		commit = self.sp_obj.getMacro(commit_label)
		if commit == "":
			commit_label = 'rev'
			commit = self.sp_obj.getMacro(commit_label)

		if commit == "":
			self.t_result = "E: missing %global commit or rev"
			self.err_number += 1
			return False

		if commit_label == "commit":
			if self.sp_obj.getMacro("shortcommit") == "":
				self.t_result = "E: missing %global shortcommit"
				self.err_number += 1
				return False
			self.t_result = "I: commit and shortcommit macro"
		else:
			if self.sp_obj.getMacro("shortrev") == "":
				self.t_result = "E: missing %global shortrev"
				self.err_number += 1
				return False
			self.t_result = "I: rev and shortrev macro"

		return True

	def testImportPathMacros(self):
		devel_prefix = self.sp_obj.getMacro('devel_prefix')
		if devel_prefix == "":
			import_path = self.sp_obj.getMacro('import_path')
			ip_macro = "import_path"
		else:
			import_path = self.sp_obj.getMacro('%s_import_path' % devel_prefix)
			ip_macro = "%s_import_path" % devel_prefix

		if import_path == "":
			self.t_result = "E: Missing %%{%s} macro" % ip_macro
			self.err_number += 1
			return False

		self.t_result = "I: %s macro found" % ip_macro
		return True

	def testRepositoryMacros(self):
		provider = self.sp_obj.getMacro('provider')
		if provider == "":
			self.t_result = "E: Missing %{provider} macro"
			self.err_number += 1
			return False

		provider_tld = self.sp_obj.getMacro('provider_tld')
		if provider_tld == "":
			self.t_result = "E: Missing %{provider_tld} macro"
			self.err_number += 1
			return False

		repo = self.sp_obj.getMacro('repo')
		if repo == "":
			self.t_result = "E: Missing %{repo} macro"
			self.err_number += 1
			return False

		project = self.sp_obj.getMacro('project')
		if project == "":
			self.t_result = "E: Missing %{project} macro"
			self.err_number += 1
			return False

		self.t_result = "I: provider, provider_tld, repo and project macros found"
		return True

	def testSpecFileSource(self):
		source = self.sp_obj.getTag('source')
		if source == "":
			self.t_result = "E: Missing source tag"
			self.err_number += 1
			return False

		archive = path.basename(source)

		if self.sources != "":
			sources = self.src_obj.getFiles()

			if archive not in sources:
				self.t_result = "E: archive in source[0] tag not in sources file"
				self.err_number += 1
				return False

		if archive != self.archive:
			self.t_result = "E: sources[0]'s tarball != specified tarball: %s != %s" % (archive, self.archive)
			self.err_number += 1
			return False

		self.t_result = "I: %s archive found in sources" % archive
		return True

	def testDevel(self):
		# get provided and imported paths from tarball
		t_imported = self.prj_obj.getImportedPackages()
		package_imports_occurence = self.prj_obj.getPackageImportsOccurences()
		t_provided = self.prj_obj.getProvidedPackages()

		# filter out import paths used only by main packages
		non_main_imported = []
		for gimport in t_imported:
			skip = True
			if gimport in package_imports_occurence:
				for occurrence in package_imports_occurence[gimport]:
					if not occurrence.endswith(":main"):
						skip = False
						break
			if skip:
				continue
			non_main_imported.append(gimport)

		t_imported = non_main_imported

		devel_prefix = self.sp_obj.getMacro('devel_prefix')
		if devel_prefix == "":
			import_path = self.sp_obj.getMacro('import_path')
		else:
			import_path = self.sp_obj.getMacro('%s_import_path' % devel_prefix)

		# import paths can be in form prefix/Godeps/_workspace/src/IMPORTED_PATH
		godeps_ips = filter(lambda i: i.startswith("%s/Godeps/_workspace/src/" % import_path), t_imported)
		godeps_ips = map(lambda i: i.replace("%s/Godeps/_workspace/src/" % import_path, ""), godeps_ips)

		t_imported = filter(lambda i: not i.startswith(import_path), t_imported)


		t_imported = map(lambda i: str("golang(%s)" % i), t_imported + godeps_ips)

		skipped_provides_with_prefix = Config().getSkippedProvidesWithPrefix()

		for provide_prefix in skipped_provides_with_prefix:
			t_provided = filter(lambda i: not i.startswith(provide_prefix), t_provided)

		# filter out all provided packages that contains /internal/ keyword
		t_provided = filter(lambda i: "internal" not in i.split("/"), t_provided)

		# add golang( prefix to all provided packages
		t_provided = map(lambda i: str("golang(%s/%s)" % (import_path, i)) if i != "." else str("golang(%s)" % import_path), t_provided)
		# get provides and [B]R from package section
		devel_obj = self.sp_obj.getDevelSubpackage()
		if devel_obj == None:
			self.t_result = "E: Unable to find devel subpackage"
			self.err_number += 1
			return False

		s_br = filter(lambda l: l != "golang", devel_obj.getBuildRequires())
		s_r = devel_obj.getRequires()
		s_provided = devel_obj.getProvides()

		# check only those br/r/provides that are related to golang
		s_br = filter(lambda l: l.startswith("golang("), s_br)
		s_r = filter(lambda l: l.startswith("golang("), s_r)
		s_provided = filter(lambda l: l.startswith("golang("), s_provided)

		# compare
		self.t_result = []
		# BR
		super_br = list(set(s_br) - set(t_imported) - set(['golang']))
		missing_br = list(set(t_imported) - set(s_br))
		for br in sorted(missing_br):
			self.t_result.append("W: Missing BuildRequires: %s" % br)
			self.warn_number += 1

		for br in sorted(super_br):
			self.t_result.append("W: Superfluous BuildRequires: %s" % br)
			self.warn_number += 1

		# R
		super_r = list(set(s_r) - set(t_imported) - set(['golang']))
		missing_r = list(set(t_imported) - set(s_r))
		for r in sorted(missing_r):
			self.t_result.append("W: Missing Requires: %s" % r)
			self.warn_number += 1

		for r in sorted(super_r):
			self.t_result.append("W: Superfluous Requires: %s" % r)
			self.warn_number += 1

		# Provides
		super_p = list(set(s_provided) - set(t_provided))
		missing_p = list(set(t_provided) - set(s_provided))
		for p in sorted(missing_p):
			self.t_result.append("W: Missing Provides: %s" % p)
			self.warn_number += 1

		for p in sorted(super_p):
			self.t_result.append("W: Superfluous Provides: %s" % p)
			self.warn_number += 1

		if self.t_result != []:
			return False

		self.t_result = "I: devel's provides checked"
		return True
示例#11
0
class PackageInfo:
	"""
	Get basic information about project:
		imported packages
		provided packages
		tests
	"""
	def __init__(self, import_path, commit = "", noGodeps = [], skip_errors=False):
		self.import_path = import_path
		self.commit = commit
		self.noGodeps = noGodeps
		self.err = ""
		self.repository_info = None
		self.project_info = None
		self.archive_dir = ""
		self.repository_decoded = False
		self.name = ""
		self.skip_errors = skip_errors

	def getError(self):
		return self.err

	def getName(self):
		return self.name

	def getRepositoryInfo(self):
		return self.repository_info

	def getProjectInfo(self):
		return self.project_info

	def decodeRepository(self):
		# get repository info
		self.repository_info = RepositoryInfo(self.import_path, self.commit)
		if not self.repository_info.retrieve():
			self.err = self.repository_info.getError()
			return False

		# package name
		ip_info = self.repository_info.getImportPathInfo()

		r_info = self.repository_info.getArchiveInfo()

		self.repository_decoded = True
		self.archive_dir = r_info.archive_dir
		self.name = ip_info.getPackageName()
		return True

	def decodeProject(self, working_directory = "."):
		if not self.repository_decoded:
			self.err = "RepositoryInfo not decoded"
			return False

		# get package info
		self.project_info = ProjectInfo(self.noGodeps)
		source_code_directory = "%s/%s" % (working_directory, self.archive_dir)
		if not os.path.exists(source_code_directory):
			self.err = "Source code directory %s does not exist." % source_code_directory
			self.err += "CWD: %s" % os.getcwd()
			return False

		if not self.project_info.retrieve(source_code_directory, self.skip_errors):
			self.err = self.project_info.getError()
			return False

	
		return True