Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

import os 

import re 

import shutil 

import string 

import hashlib 

import subprocess 

 

from lutris.util.log import logger 

 

 

def execute(command): 

    """ Execute a system command and result its results """ 

    stdout, stderr = subprocess.Popen(command, 

                                      stdout=subprocess.PIPE, 

                                      stderr=subprocess.PIPE).communicate() 

    return stdout.strip() 

 

 

def calculate_md5(filename): 

    """ Return the md5 hash of filename. """ 

    md5 = hashlib.md5() 

    try: 

        with open(filename, 'rb') as f: 

            for chunk in iter(lambda: f.read(8192), b''): 

                md5.update(chunk) 

    except IOError: 

        print "Error reading %s" % filename 

        return False 

    return md5.hexdigest() 

 

 

def find_executable(exec_name): 

    if not exec_name: 

        raise ValueError("find_executable: exec_name required") 

    return execute(['which', exec_name]) 

 

 

def get_pid(program): 

    return execute(['pgrep', program]) 

 

 

def kill_pid(pid): 

    assert str(int(pid)) == str(pid) 

    execute(['kill', '-9', pid]) 

 

 

def get_cwd(pid): 

    cwd_file = '/proc/%d/cwd' % int(pid) 

    if not os.path.exists(cwd_file): 

        return False 

    return os.readlink(cwd_file) 

 

 

def get_command_line(pid): 

    cmdline_path = '/proc/%d/cmdline' % int(pid) 

    if not os.path.exists(cmdline_path): 

        return False 

    return open(cmdline_path, 'r').read().replace('\x00', ' ').strip() 

 

 

def python_identifier(string): 

    if not isinstance(string, basestring): 

        logger.error("python_identifier requires a string, got %s", string) 

        return 

 

    def dashrepl(matchobj): 

        return matchobj.group(0).replace('-', '_') 

 

    return re.sub(r'(\${)([\w-]*)(})', dashrepl, string) 

 

 

def substitute(fileid, files): 

    fileid = python_identifier(str(fileid)) 

    files = dict((k.replace('-', '_'), v) for k, v in files.items()) 

    template = string.Template(fileid) 

    if fileid in files.keys(): 

        return files[fileid] 

    return template.safe_substitute(files) 

 

 

def merge_folders(source, destination): 

    logger.debug("Merging %s into %s", source, destination) 

    for (dirpath, dirnames, filenames) in os.walk(source): 

        source_relpath = dirpath[len(source) + 1:] 

        dst_abspath = os.path.join(destination, source_relpath) 

        for dirname in dirnames: 

            new_dir = os.path.join(dst_abspath, dirname) 

            logger.debug("creating dir: %s" % new_dir) 

            try: 

                os.mkdir(new_dir) 

            except OSError: 

                pass 

        for filename in filenames: 

            logger.debug("Copying %s" % filename) 

            if not os.path.exists(dst_abspath): 

                os.makedirs(dst_abspath) 

            shutil.copy(os.path.join(dirpath, filename), 

                        os.path.join(dst_abspath, filename)) 

 

 

def is_removeable(path, excludes=None): 

    """ Given a folder path, tells if it safe to remove it """ 

    if not path: 

        return False 

    if not os.path.exists(path): 

        return False 

    if path in excludes: 

        return False 

 

    parts = path.strip('/').split('/') 

    if parts[0] in ('usr', 'var', 'lib', 'etc', 'boot', 'sbin', 'bin'): 

        # Path is part of the system folders 

        return False 

 

    if parts[0] == 'home' and len(parts) == 2: 

        # Path is a home folder 

        return False 

 

    return True