Seite 1 von 1

Bin neue und hab eine Frage

Verfasst: Montag 29. Februar 2016, 19:24
von Gamers RS
Hi Community,

bin neue auf dem Gebiet und würde gerne einen Pythonscript so umschreiben, wenn man zb. im Linux folgende Zeile einfügt dass diese direkt zu einem Line springt, wenn jedoch andere eingeben dann sollte es auf andere Line springen.

Bsp. wäre wenn ich eine 500GB oder eine 1TB Festplatte formatieren und partitionieren will :)

sudo python ./hdd.py sdc 500

Wie kann ich es so machen dass im einem Script die eingefügt werden können?

Danke vorab für die Hilfen :)

Re: Bin neue und hab eine Frage

Verfasst: Montag 29. Februar 2016, 19:34
von __deets__
Das was du suchst ist wahrscheinlich ein Kommandozeilen-Argument. Die findet man in Python in sys.argv:

Code: Alles auswählen

from __future__ import print_function
import sys
print(sys.argv)
Einfach mal mit verschieden Argumenten aufrufen.

Wenn das ganze dann etwas bequemer, und mit automatischer Validierung/Hilfetexten sein soll, dann empfiehlt sich das Modul argparse.

Re: Bin neue und hab eine Frage

Verfasst: Montag 29. Februar 2016, 19:38
von Gamers RS
Danke. Sorry aber ich bin wirklich ein Newbie :) CMD bin ich besser drin als in Python.

Das ist das Script was ich gerne umschreiben will

Code: Alles auswählen

#!/usr/bin/python

import os
from stat import *
from os import path
import sys
import gptutil


DISK_GUID           = 'B2A1E8252A0B744493FA35B847D97EE5'.decode('hex')
TEMP_CONTENT_GUID   = 'A57D72B3ACA33D4B9FD62EA54441011B'.decode('hex')
USER_CONTENT_GUID   = 'E0B59B865633E64B85F729323A675CC7'.decode('hex')
SYSTEM_SUPPORT_GUID = '477A0DC9B9CCBA4C8C660459F6B85724'.decode('hex')
SYSTEM_UPDATE_GUID  = 'D76A059AED324141AEB1AFB9BD5565DC'.decode('hex')
SYSTEM_UPDATE2_GUID = '7C19B224019DF945A8E1DBBCFA161EB2'.decode('hex')

PARTITION_SIZES = [
    44023414784,
    53687091200,
    42949672960,
    12884901888,
    7516192768
]


def print_parted_commands(device):
    temp_end = 1 + (PARTITION_SIZES[0]/1024/1024)
    user_end = temp_end + (PARTITION_SIZES[1]/1024/1024)
    sys_end = user_end + (PARTITION_SIZES[2]/1024/1024)
    upt_end = sys_end + (PARTITION_SIZES[3]/1024/1024)
    upt2_end = upt_end + (PARTITION_SIZES[4]/1024/1024)

    f = open('mkxboxfs.sh', 'w')
    f.write('#!/bin/bash\n')
    f.write('DEV={0}\n'.format(device))
    f.write('parted -s "$DEV" mktable gpt\n')
    f.write('parted -s "$DEV" mkpart primary ntfs 1.00MiB {0}MiB\n'.format(temp_end))
    f.write('parted -s "$DEV" name 1 "\\"Temp Content\\""\n')
    f.write('mkntfs -q "${DEV}1" -f -L "Temp Content"\n')
    f.write('parted -s "$DEV" mkpart primary ntfs {0}MiB {1}MiB\n'.format(temp_end, user_end))
    f.write('parted -s "$DEV" name 2 "\\"User Content\\""\n')
    f.write('mkntfs -q "${DEV}2" -f -L "User Content"\n')
    f.write('parted -s "$DEV" mkpart primary ntfs {0}MiB {1}MiB\n'.format(user_end, sys_end))
    f.write('parted -s "$DEV" name 3 "\\"System Support\\""\n')
    f.write('mkntfs -q "${DEV}3" -f -L "System Support"\n')
    f.write('parted -s "$DEV" mkpart primary ntfs {0}MiB {1}MiB\n'.format(sys_end, upt_end))
    f.write('parted -s "$DEV" name 4 "\\"System Update\\""\n')
    f.write('mkntfs -q "${DEV}4" -f -L "System Update"\n')
    f.write('parted -s "$DEV" mkpart primary ntfs {0}MiB {1}MiB\n'.format(upt_end, upt2_end))
    f.write('parted -s "$DEV" name 5 "\\"System Update 2\\""\n')
    f.write('mkntfs -q "${DEV}5" -f -L "System Update 2"\n')
    f.flush()
    f.close()
    os.chmod('mkxboxfs.sh', 0o777)


def fixup_header(hdr):
    hdr.disk_guid = DISK_GUID
    hdr.fix_crc()


def fixup_part_table(pt):
    pt.partitions[0].part_guid = TEMP_CONTENT_GUID
    pt.partitions[0].name = u'Temp Content'
    pt.partitions[1].part_guid = USER_CONTENT_GUID
    pt.partitions[1].name = u'User Content'
    pt.partitions[2].part_guid = SYSTEM_SUPPORT_GUID
    pt.partitions[2].name = u'System Support'
    pt.partitions[3].part_guid = SYSTEM_UPDATE_GUID
    pt.partitions[3].name = u'System Update'
    pt.partitions[4].part_guid = SYSTEM_UPDATE2_GUID
    pt.partitions[4].name = u'System Update 2'

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print 'Usage:'
        print '\t{0} [disk]'.format(sys.argv[0])
        print 'Example:'
        print '\t{0} sdf'.format(sys.argv[0])
        print
        sys.exit(-1)

    # open the disk
    _path = path.join('/dev', sys.argv[1])
    disk = gptutil.Disk.from_path(_path)
    partitions = disk.header.partition_table.active_partitions

    # calculate user partition size to nearest GiB
    total_size = int(open(path.join('/sys', 'class', 'block', sys.argv[1], 'size'), 'r').readline()) * 512
    user_content_size = (total_size - sum(PARTITION_SIZES))/1024/1024/1024
    PARTITION_SIZES[1] = user_content_size*1024*1024*1024

    # verify partition count
    if len(partitions) != 5:
        print 'Disk must have 5 partitions'
        print 'Create as follows:'
        print '\t41 GiB NTFS'
        print '\t{0} GiB NTFS'.format(user_content_size)
        print '\t40 GiB NTFS'
        print '\t12 GiB NTFS'
        print '\t7 GiB NTFS'
        print_parted_commands(_path)
        print 'run ./mkxboxfs.sh to create the correct partitions'
        sys.exit(-2)

    # verify partition sizes
    for i in range(5):
        correct = PARTITION_SIZES[i]
        actual = partitions[i].size
        if correct != actual:
            print 'Partition {0} must be EXACTLY {1} bytes!'.format(i, correct)
            print 'It is {0} bytes'.format(actual)
            print_parted_commands(_path)
            print 'run ./mkxboxfs.sh to create the correct partitions'
            sys.exit(-3)

    # confirm actions
    print 'The actions performed CANNOT be reversed!'
    print 'Are you SURE you want to convert {0} to an Xbox ONE Disk?'.format(_path)
    s = raw_input("Enter 'yes' to continue: ")
    if s != 'yes':
        sys.exit(-4)

    # change partition table and backup partition table
    fixup_part_table(disk.header.partition_table)

    # change header and backup header
    fixup_header(disk.header)

    print 'Writing changes to disk...'
    diskf = open(_path, 'rb+')
    disk.commit(f=diskf)
    print 'Changes Written!'

Re: Bin neue und hab eine Frage

Verfasst: Montag 29. Februar 2016, 19:41
von BlackJack
@Gamers RS: Zu ”Zeilen springen” geht in Python, wie in jeder anderen modernen, strukturierten Programmiersprache nicht, das macht man auch schon seit Jahrzehnten nicht mehr so. Selbst in Sprachen in denen man das früher so gemacht hat, ich glaube BASIC ist hier die Referenz, gibt es in modernen Dialekten meistens nicht einmal die Möglichkeit.

Du möchtest wahrscheinlich einfach mal ein Python-Grundlagentutorial durcharbeiten um die Kontrollstrukturen kennen zu lernen die Python unter anderem für bedingte Ausführung zur Verfügung stellt.

Für die Auswertung von Kommandozeilenargumenten gibt es in der Standardbibliothek das `argparse`-Modul.

Re: Bin neue und hab eine Frage

Verfasst: Montag 29. Februar 2016, 19:51
von Gamers RS
So ungefähr. argparse ist ja wahrscheinlich das Richtige :) Danke

Ich würde zb. --500 Eingabe eine Master GUID auswählen können und bei --1000 die andere Master GUID. Sowas ist doch möglich?