~usb-creator-hackers/usb-creator/trunk

« back to all changes in this revision

Viewing changes to usbcreator/misc.py

  • Committer: Brian Murray
  • Date: 2021-04-02 15:52:52 UTC
  • Revision ID: brian@canonical.com-20210402155252-tk5d4a6lczeld0e0
Tags: 0.3.9
releasing package usb-creator version 0.3.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import subprocess
21
21
import sys
22
22
 
23
 
# The minimum size, in megabytes a persistence file can be.
24
 
MIN_PERSISTENCE = 1024
25
 
MAX_PERSISTENCE = 4096 # VFAT maximum file size.
26
23
# Padding for the kernel and initramfs, in megabytes
27
24
PADDING = 30
28
25
(CAN_USE,     # Obvious.
35
32
MAX_LOG_SIZE = 1024 * 1024 * 1
36
33
MAX_LOG_BACKUP = 0
37
34
 
38
 
EULA_STAMP = '.ubuntu-nexus7-installer.notice-accepted'
39
 
 
40
 
if sys.platform != 'win32':
41
 
    # TODO xnox 20121109 should not hard-code timeouts, instead should
42
 
    # do async dbus calls with a qt or glib main loop running.
43
 
    MAX_DBUS_TIMEOUT = 2147483
 
35
# TODO xnox 20121109 should not hard-code timeouts, instead should
 
36
# do async dbus calls with a qt or glib main loop running.
 
37
MAX_DBUS_TIMEOUT = 2147483
44
38
 
45
39
if sys.version >= '3':
46
40
    text_type = str
80
74
        log.addHandler(handler)
81
75
    log.setLevel(logging.DEBUG)
82
76
 
83
 
def check_eula(save=False):
84
 
    eula_stamp = os.path.expanduser('~/' + EULA_STAMP)
85
 
    if save:
86
 
        with open(eula_stamp, 'w') as f:
87
 
            pass
88
 
    return os.path.isfile(eula_stamp)
89
 
    
90
77
def format_size(size):
91
78
    """Format a partition size."""
92
79
    # Taken from ubiquity's ubiquity/misc.py
120
107
        factor = 1024 * 1024
121
108
    return '%.1f %s' % (float(size) / factor, unit)
122
109
 
123
 
def fs_size(device):
124
 
    '''Returns a tuple of the total size of the filesystem
125
 
       and the free space on it.'''
126
 
    # FIXME evand 2009-06-05: Do we want the free bytes available to the user,
127
 
    # or the total free bytes?  Right now we're using the latter.
128
 
 
129
 
    if sys.platform == 'win32':
130
 
        # Taken from Wubi.
131
 
        import ctypes
132
 
        freeuser = ctypes.c_int64()
133
 
        total = ctypes.c_int64()
134
 
        free = ctypes.c_int64()
135
 
        try:
136
 
            ctypes.windll.kernel32.GetDiskFreeSpaceExW(
137
 
                    text_type(device),
138
 
                    ctypes.byref(freeuser),
139
 
                    ctypes.byref(total),
140
 
                    ctypes.byref(free))
141
 
        except:
142
 
            return (0, 0)
143
 
        return (total.value, free.value)
144
 
    else:
145
 
        try:
146
 
            stat = os.statvfs(device)
147
 
        except:
148
 
            return (0, 0)
149
 
        free = stat.f_bsize * stat.f_bavail # Include reserved blocks.
150
 
        total = stat.f_bsize * stat.f_blocks
151
 
        return (total, free)
152
 
 
153
110
class USBCreatorProcessException(Exception):
154
111
    pass
155
112
 
156
113
def popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
157
114
          stdin=subprocess.PIPE):
158
115
    logging.debug(str(cmd))
159
 
    if sys.platform == 'win32':
160
 
        STARTF_USESHOWWINDOW = 1
161
 
        SW_HIDE = 0
162
 
        startupinfo = subprocess.STARTUPINFO()
163
 
        startupinfo.dwFlags |= STARTF_USESHOWWINDOW
164
 
        startupinfo.wShowWindow = SW_HIDE
165
 
    else:
166
 
        startupinfo = None
 
116
    startupinfo = None
167
117
    process = subprocess.Popen(cmd, stdout=stdout, stderr=stderr, stdin=stdin,
168
118
                               universal_newlines=True,
169
119
                               startupinfo=startupinfo)