コード例 #1
0
class HButtons:

    # code block that initializes HButtons class()
    def __init__(self, master):

        global disk
        global disks

        disks = {False: [], True: []}
        for disk in loading(dev_detect('diskutil list -plist'))['WholeDisks']:
            disks[loading(dev_detect('diskutil info -plist ' + disk))['Internal']].append(disk)

        # second thread defined -- for dcfldd background operation
        thread_flash = threading.Thread(target=self.flash)
        thread_flash.daemon = True

        # main button frame -- frame packed with uSD check_Buttons, FLASH and FILE select buttons
        frame = Frame(master, bg="whitesmoke")
        frame.pack(pady=5, expand=1)

        progress_var = DoubleVar()
        self.MAX = 150

        self.progress = Progressbar(frame, orient=HORIZONTAL, length=115, mode="determinate", variable=progress_var)
        self.progress.start()

        # most buttons are coded here
        # image button and flash button code
        self.imgButton = Button(frame, text="File to duplicate", font="Tahoma", command=self.filebox,
                                highlightbackground="whitesmoke").pack(side=TOP, padx=20, pady=17)
        self.flashButton = Button(frame, text="Flash", font="Tahoma", command=thread_flash.start,
                                  highlightbackground="whitesmoke", fg="brown").pack(side=BOTTOM, padx=20, pady=7)

        # checkbutton code block

        self.u1 = Checkbutton(frame, text="uSD1", font="Tahoma", var=chk_state, command=self.usdsel1,
                              background="coral")
        self.u2 = Checkbutton(frame, text="uSD2", font="Tahoma", var=chk_state2, command=self.usdsel2,
                              bg="coral")
        self.u3 = Checkbutton(frame, text="uSD3", font="Tahoma", var=chk_state3, command=self.usdsel3,
                              bg="coral")
        self.u4 = Checkbutton(frame, text="uSD4", font="Tahoma", var=chk_state4, command=self.usdsel4,
                              bg="coral")

        # [self.u1.configure(state='active') if disk == "disk2" else self.u1.config(state='disabled')
        # for disks in disks]

        self.u1.pack(padx=35, pady=2, fill="x")
        self.u2.pack(padx=35, pady=2, fill="x")
        self.u3.pack(padx=35, pady=2, fill="x")
        self.u4.pack(padx=35, pady=2, fill="x")

        self.u1.invoke()
        self.u1.invoke()

        self.u2.invoke()
        self.u2.invoke()

        self.u3.invoke()
        self.u3.invoke()

        self.u4.invoke()
        self.u4.invoke()

        # [card_ind2.pack() if disk == "disk3" else card_ind2.pack_forget() for disks in disks]

        # [card_ind3.pack() if disk == "disk4" else card_ind3.pack_forget() for disks in disks]

        # [card_ind4.pack() if disk == "disk5" else card_ind4.pack_forget() for disks in disks]

    def invoke(self):
        self.u1.invoke()
        self.u1.invoke()

        self.u2.invoke()
        self.u2.invoke()

        self.u3.invoke()
        self.u3.invoke()

        self.u4.invoke()
        self.u4.invoke()

    def progress_f(self):
        k = 0
        while k <= self.MAX:
            self.progress_var.set(k)
            k += 1
            time.sleep(0.02)
            root.update()
        root.after(100, self.progress_f)

    def flash(self):

        self.progress.pack_configure(side=BOTTOM, expand=0, pady=10, padx=10)

        stoprequest.set()

        selection = str(root.filename)
        img = selection[:-28][+25:]

        dev_1 = '2'
        dev_2 = '3'
        dev_3 = '4'
        dev_4 = '5'

        bish = "#!/bin/bash\n\n"

        y = "sudo dcfldd sizeprobe=if "
        x = "if={} ".format(img)

        a = "of=/dev/rdisk{} ".format(dev_1)
        b = "of=/dev/rdisk{} ".format(dev_2)
        g = "of=/dev/rdisk{} ".format(dev_3)
        h = "of=/dev/rdisk{} ".format(dev_4)

        za = "diskutil unmountDisk /dev/disk{}\n".format(dev_1)
        zb = "diskutil unmountDisk /dev/disk{}\n".format(dev_2)
        zg = "diskutil unmountDisk /dev/disk{}\n".format(dev_3)
        zh = "diskutil unmountDisk /dev/disk{}\n".format(dev_4)

        spc = "\n"
        spc2 = "\n\n"

        global bash_cmd

        if chk_state.get() == TRUE:
            bash_cmd = ("{}{}{}{}{}{}{}{}".format(bish, za, spc, y, x, a, spc2, za))
        if chk_state2.get() == TRUE:
            bash_cmd = ("{}{}{}{}{}{}{}{}".format(bish, zb, spc, y, x, b, spc2, zb))
        if chk_state3.get() == TRUE:
            bash_cmd = ("{}{}{}{}{}{}{}{}".format(bish, zg, spc, y, x, g, spc2, zg))
        if chk_state4.get() == TRUE:
            bash_cmd = ("{}{}{}{}{}{}{}{}".format(bish, zh, spc, y, x, h, spc2, zh))
        if chk_state.get() & chk_state2.get() == TRUE:
            bash_cmd = ("{}{}{}{}{}{}{}{}{}{}{}".format(bish, za, zb, spc, y, x, a, b, spc2, za, zb))
        if chk_state.get() & chk_state3.get() == TRUE:
            bash_cmd = ("{}{}{}{}{}{}{}{}{}{}{}".format(bish, za, zg, spc, y, x, a, g, spc2, za, zg))
        if chk_state.get() & chk_state4.get() == TRUE:
            bash_cmd = ("{}{}{}{}{}{}{}{}{}{}{}".format(bish, za, zh, spc, y, x, a, h, spc2, za, zh))
        if chk_state2.get() & chk_state3.get() == TRUE:
            bash_cmd = ("{}{}{}{}{}{}{}{}{}{}{}".format(bish, zb, zg, spc, y, x, b, g, spc2, zb, zg))
        if chk_state2.get() & chk_state4.get() == TRUE:
            bash_cmd = ("{}{}{}{}{}{}{}{}{}{}{}".format(bish, zb, zh, spc, y, x, b, h, spc2, zb, zh))
        if chk_state3.get() & chk_state4.get() == TRUE:
            bash_cmd = ("{}{}{}{}{}{}{}{}{}{}{}".format(bish, zg, zh, spc,  y, x, g, h, spc2, zg, zh))
        if chk_state.get() & chk_state2.get() & chk_state3.get() == TRUE:
            bash_cmd = ("{}{}{}{}{}{}{}{}{}{}{}{}{}{}".format(bish, za, zb, zg, spc, y, x, a, b, g, spc2, za, zb, zg))
        if chk_state.get() & chk_state2.get() & chk_state4.get() == TRUE:
            bash_cmd = ("{}{}{}{}{}{}{}{}{}{}{}{}{}{}".format(bish, za, zb, zh, spc, y, x, a, b, h, spc2, za, zb, zh))
        if chk_state.get() & chk_state3.get() & chk_state4.get() == TRUE:
            bash_cmd = ("{}{}{}{}{}{}{}{}{}{}{}{}{}{}".format(bish, za, zg, zh, spc, y, x, a, g, h, spc2, za, zg, zh))
        if chk_state2.get() & chk_state3.get() & chk_state4.get() == TRUE:
            bash_cmd = ("{}{}{}{}{}{}{}{}{}{}{}{}{}{}".format(bish, zb, zg, zh, spc, y, x, b, g, h, spc2, zb, zg, zh))
        if chk_state.get() & chk_state2.get() & chk_state3.get() & chk_state4.get() == TRUE:
            bash_cmd = ("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}".format(bish, za, zb, zg, zh, spc, y, x, a, b, g, h, spc2,
                                                                    za, zb, zg, zh))
        combo_fmt = ''.join(map(str, bash_cmd))

        dd_exe_f = open("/Users/hunterhartline/PycharmProjects/uSDgui/execute.sh", "w")

        for i in range(1):
            dd_exe_f.write(combo_fmt)
        dd_exe_f.close()

        print("")
        print(".")
        time.sleep(.5)

        print("..")
        time.sleep(.5)

        print("...")
        time.sleep(.5)

        print(" ^^^^^^^^^^^^^^^^^^^^ ")
        print(" ^!flashing card(s)!^ ")
        print(" ^^^^^^^^^^^^^^^^^^^^ ")

        # self.subprocess_flash() function was demoted to nest in flash() -- this function you're perusing now
        dd = subprocess.Popen(['/Users/hunterhartline/PycharmProjects/uSDgui/execute.sh'], stdin=subprocess.PIPE,
                                   stdout=subprocess.PIPE)
        output = dd.communicate()[0].decode('utf-8')

        # output printed for debugging
        print(output)

        if chk_state8.get() == TRUE:
            print("...")
            print("..")
            print(".")
            print("program will auto-close in: 3 sec --->change in preferences<---")
            time.sleep(3)
            root.destroy()
        elif chk_state8.get() == FALSE:
            print("Done flashing cards!")

    def usdsel1(self):
        if chk_state.get() == TRUE:
            st1 = "uSD1"

        elif chk_state.get() == FALSE:
            st1 = "---"
        us1.config(text=str(st1))

    def usdsel2(self):

        if chk_state2.get() == TRUE:
            st2 = "uSD2"

        elif chk_state2.get() == FALSE:
            st2 = "---"
        us2.config(text=str(st2))

    def usdsel3(self):
        if chk_state3.get() == TRUE:
            st3 = "uSD3"

        elif chk_state3.get() == FALSE:
            st3 = "---"
        us3.config(text=str(st3))

    def usdsel4(self):
        if chk_state4.get() == TRUE:
            st4 = "uSD4"

        elif chk_state4.get() == FALSE:
            st4 = "---"
        us4.config(text=str(st4))

    def filebox(self):
        cs = vp.get()
        dp = "/"
        self.ch = ""

        if chk_state6.get() == TRUE:
            self.ch = cs
        elif chk_state6.get() == FALSE:
            self.ch = dp

        root.filename = filedialog.askopenfile(initialdir=self.ch,
                                               title="Select image to duplicate",
                                               filetypes=(("image files", "*.img"), ("all files", "*.*")))
        selection = str(root.filename)

        if selection == "None":
            selfile.config(text="-----")

            chk_state.set(False)
            chk_state2.set(False)
            chk_state3.set(False)
            chk_state4.set(False)
            self.invoke()

        elif selection != "None" and chk_state5.get() == TRUE:
            selfile.config(text=os.path.basename(os.path.normpath(selection))[:-28])

            chk_state.set(True)
            chk_state2.set(True)
            chk_state3.set(True)
            chk_state4.set(True)
            self.invoke()

        elif selection != "None" and chk_state5.get() == FALSE:
            selfile.config(text=os.path.basename(os.path.normpath(selection))[:-28])

            chk_state.set(False)
            chk_state2.set(False)
            chk_state3.set(False)
            chk_state4.set(False)
            self.invoke()